This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

unnecessary template bloat


When I compile the following program with gcc 3.2 using
g++ -Os -fomit-frame-pointer -fno-default-inline -fno-exceptions -fno-rtti
-W -Wall -S temp.cpp

--------------temp.cpp

template<int size_> 
class array
{
public:
  int size;
  int data[size_];
  array():size(size_){};
  void init0(){for (int i=0; i<size; ++i)data[i]=0;};
};

int main()
{
  array<1> a0;
  array<2> a1;
  array<3> a2;
  a0.init0();
  a1.init0();
  a2.init0();
}
----------------

gcc generates 3 identical init0 functions although it should be easy to
recognize that init0 does not depend on the template argument. Is there a
way I can prevent gcc from doing this?
The best workaround I could come up with is:

class arraybase
{
public:
  int size;
  int data[];
  void init0(){for (int i=0; i<size; ++i)data[i]=0;};
};

template<int size_> 
class array:public arraybase
{
public:
  int data[size_];
  array(){size=size_;};
};


but I'm pretty sure this is not at all portable because there's no
guarantee that array's data and arraybase's data will really end up at the
same address.

MSB

-- 
Support bacteria - they're the only culture some people have.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]