🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #808

Re: Templates

#808
From: Albert Golembiowski
Date: Mon, 30 Oct 1995 22:50
124 lines
4750 bytes
ag> From: andrew@tagsys.com (Andrew Gideon) writes:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ag> In article AA05482@brie, albert@inrs-telecom.uquebec.ca (Albert Golembiowski) writes:
ag> >Of course it's possible with g++-2.7.0 to include only interface headers,
ag> >templates or not. To carry along template implementations, as suggested
ag> >below, is to create dependency nightmare, i.e. any change
ag> >in `mytemplates.cc' might put you through the amazing experience of watching
ag> >the whole world recompile!
ag> >
ag> >Not to mention that doing this with any sizable project will make your
ag> >compiler eat all the available memory, then proceed to your pets, then ...:-)
ag> >
ag> 
ag> Well, I don't want to lose my cats to a greedy compiler.

This example is just to illustrate, and it won't compile. I carry
template stuff in 3 files

          Vect.hh - interface
          Vect.C  - implementation
          Vect.cc - instantiations

Then you say 

            `g++-2.7.0 -c -O3 -fno-implicit-templates Vect.cc'

and the instantiations will sit in Vect.o. You can stick it into an
archive to link with. An alternative is the repository method (`-repo'
flag),  which I haven't used yet, but which apparently besides
relieving you from remembering to do manually the instantiations you
need, carries a exec size bonus.
The `-fno-implicit-templates' creats deadwood: it instantiaties all
the methods in Vect.hh and sticks them in the exec whether you need them or not.
The `-repo' method does it apparently more selectively.

------------------------------ Vect.hh -------------------------------------
#ifndef Vect_hh_
#define Vect_hh_

class ostream;

template <class Type> class Vect_;                // for `friend'

template <class Type> class Vect
{//___________________~~~~~~~~~~
friend 
  ostream& operator<<(ostream& O, const Vect<Type>& V);
  typedef Vect SELF;
public:
  typedef Type TYPE;

public:    // ......ctors
  Vect (int Sz = 1)                { init(TYPE(0),Sz);      }
  Vect (const TYPE& val, int Sz)   { init(val, Sz);         }

  int     Size   () const { return size; }
  virtual bool   isZero () const;                          //null or all element equall zero

public:
  // ...  trim to exactly size 'Sz' (may be 0) or strech (with zero's); preserves old values
  SELF&  _ReSize (int Sz, bool Clr=false); 

protected:        
  int  size;      
  TYPE *elem;     

private:
  void init (const TYPE* V, int Sz, int len = 0);
  void init (const TYPE& V, int Sz, int len = 0);
};

template <class Type> inline
bool
Vect<Type>:: isZero () const
{//__________~~~~~~~~~~~~~~~~
  if (!size) return true;
  for (int i = 0; i < size; i++) if (elem[i] != (TYPE)0) return false;
  return true;
}

template <class Type> ostream& operator<<(ostream& O, const Vect<Type>& V);

----------------------------------- Vect.C -----------------------------------
#ifndef Vect_C_
#define Vect_C_

template <class Type>
Vect<Type>&
Vect<Type>:: _ReSize (int Sz, bool Clr)
{//__________~~~~~~~~~~~~~~~~~~~~~~~~~~
  if (Sz == size || Sz < 0) { 
    if (Clr) for (int i = 0; i < size; i++) elem[i] = (Type)0;
    return *this;
  }
  if (!Sz) return *this = SELF(0);
  if (Clr) return *this = SELF(Sz);
  SELF v = *this;
  *this = SELF(Sz);                               // already zero
  int sz = size > v.size ? v.size : size;         // Min
  for (int i = 0; i < sz; i++) elem[i] = v.elem[i]; // copy
  return *this;
}

#define instantiate_Vect(T) \
template class Vect<T>; \
template ostream& operator << (ostream&, const Vect<T>& );

#endif
------------------------------ Vect.cc -------------------------------
#include "Vect.C" 

instantiate_Vect(char);
instantiate_Vect(bool);
instantiate_Vect(int);
instantiate_Vect(float);
instantiate_Vect(Vect<int>);
instantiate_Vect(Vect<float>);
--------------------------------------------------------------------

-- 
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Albert Golembiowski      albert@inrs-telecom.uquebec.ca         (514) 762-8655
INRS Telecommunications, 16 Place du Commerce, Verdun, Quebec, Canada, H3E 1H6
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Technical Details
Message-ID: <9510302050.AA07385@brie>
Path: arc1-ca.pugleaf.net!inrs-telecom.uquebec.ca!albert
References: <471ltm$vtd@penny.tagsys.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!inrs-telecom.uquebec.ca!albert
From: albert@inrs-telecom.uquebec.ca (Albert Golembiowski)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: Re: Templates
Date: 30 Oct 1995 17:50:10 -0500
Organization: Gatewayed from the GNU Project mailing list help-g++@prep.ai.mit.edu
Sender: daemon@cis.ohio-state.edu
Distribution: world
Message-ID: <9510302050.AA07385@brie>
References: <471ltm$vtd@penny.tagsys.com>