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]

Re: The future C++ template model in gcc


> Florian Schintke wrote:
> 
> > My idea is to not instantiate the templates,
> > but to provide a generic template implementation that works
> > for all template arguments.
> In general this is not possible, consider (on an ILP=32 machine)
> 	template <typename T> int Foo () {return sizeof (T);}
> 	Foo<char> () // return 1
> 	Foo<int> () // return 4
> 	Foo<void *> () // return 4
> 
> Foo<char> must have a different instantiation than Foo<int> and Foo<void
> *>,
> but Foo<int> and Foo<void *> can have the same body. (This is what
> you mean, right?)
> 
> Proving that the body can be shared like this, can be rather tricky.
> The easiest solution might be have the linker remove copies by comparing
> the bodies directly (or a fingerprint thereof).

Instead of asking the compiler to prove that template bodies can be
shared, the library developer should use partial specialization to
make the sharing explicit.  One such approach is to produce something
like

template<size_t foo_data_size>
class Foo_POD { ... };

and then Foo<T> would be privately derived from Foo_POD<sizeof(T)> for
the cases where T is a POD.  This would less Foo<int> and Foo<void*>
share instantiations on a 32-bit machine but not on a 64-bit machine.



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