This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
unnecessary template bloat
- From: Matthias Benkmann <matthias at winterdrache dot de>
- To: gcc at gcc dot gnu dot org
- Date: Sat, 16 Nov 2002 11:41:50 +0100
- Subject: 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.