alias and template instantiations (Re: Any word on the return-type const syntax? [OT])

Bill Baxter dnewsgroup at billbaxter.com
Sun Dec 9 11:10:21 PST 2007


guslay wrote:
> Bill Baxter Wrote:
> 
>> Sean Kelly wrote:
>>>> Template member functions are not instantiated until they are called.
>>> Or until they are aliased, to be pedantic.
>> It's an important point though.  Does C++ behave that way?  I was under 
>> the impression that in C++ I could create typedefs of templated structs 
>> and classes that wouldn't get instantiated unless actually used.
> 
> 
> Instantiation is not required with typedef, because the following is legal C++
>  class Foo;
>  typedef vector<Foo> vfoo_t; // 1
> 
> but not 
>  vector<Foo> vfoo; // 2 - cannot instantiate 
> 
> I don't know however if an implementation *would* instantiate //1 if it *could* (ie. if Foo was defined).
> 
> 

I think you are correct.  Also C++ has a different way to do explicit 
instantiations:

template<class T> class Array { void mf(); };
template class Array<char>;      // explicit instantiation
template void Array<int>::mf();  // explicit instantiation

They wouldn't need that syntax if typedefs already instantiated.


The instatiate-if-aliased behavior of D is a little annoying because it 
means you can't create convenience aliases of your templates without 
bloating up your executable.  For linear algebra types, for instance, 
many C++ libraries will provide typedefs for the most common 
combinations of floating point type and number of components, maybe a 
dozen or so of these.

--bb



More information about the Digitalmars-d mailing list