This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: The future C++ template model in gcc
- To: nathan at cs dot bris dot ac dot uk
- Subject: Re: The future C++ template model in gcc
- From: Joe Buck <jbuck at synopsys dot COM>
- Date: Mon, 30 Jul 2001 09:24:34 -0700 (PDT)
- Cc: schintke at zib dot de (Florian Schintke), gdr at codesourcery dot com (Gabriel Dos Reis), gcc at gcc dot gnu dot org
> 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.