Re: specialized class template point of instantiation

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Tom Widmer (tom_usenet_at_hotmail.com)
Date: 02/24/05


Date: Thu, 24 Feb 2005 14:59:48 +0000

ben wrote:
>
> In the following program, why won't member _static MyTypeInfo
> TypeInfoBase<A>::TI_ be instantiated?
>
> Ben
>
>
> /////////////////////////////////
>
> #include <list>
> #include <iostream>
> #include <typeinfo>
>
> using namespace std;
>
> list<MyTypeInfo&> ListOfTypes;
>
> struct MyTypeInfo // purpose: to record a type
> {
> type_info& tid;
>
> MyTypeInfo(type_info& ti)
> :tid(ti)
> {
> ListOfTypes.push_back(*this);
> cout << ti.name();
> }
> };
>
> template <typename T>
> class TypeInfoBase
> {
> private:
> TypeInfoBase();
>
> public:
> *static typename MyTypeInfo TI;*

"typename" is not required there, and possibly not even allowed there
according to the standard.

>
> };
>
> *template <typename T>
> * *MyTypeInfo TypeInfoBase<T>::TI = MyTypeInfo(typeid(T));*
>
> template <typename T>
> class TypeTrait{};
>
> class A
> {
> //...
> };
>
> // specialize TypeTrait<A> to construct the static object
> *template <> class TypeTrait<A>: public TypeInfoBase<A>{};*

Why do you think that would construct the static object?

>
> int main(int argc, char* argv[])
> {
> // TypeInfoBase<A> should have been instantiated at this point
> // and so should TypeInfoBase<A>::TI be constructed.
> // But it is not! Why??

Static members of templates are only implicitly instantiated *if they
are used in a context that requires a definition*, just as with all
implicit instantiation (see 14.7.1/1). So you can trigger it by doing
something like:

MyTypeInfo const& dummy = TypeInfoBase<A>::TI;

or you can explicitly instantiate it. e.g. at namespace scope, something
like:

template MyTypeInfo TypeInfoBase<A>::TI;

but simply instantiating the class template *won't* instantiate its members.

Tom



Relevant Pages

  • Re: avoid cast to derived
    ... userBase objects, ... If wants to invoke methodfor all members of it must navigate the R2 relationship. ... So the real problem is that [Client] is instantiating two relationships here when passing a Base reference, one of which is conditional. ... As a practical matter one wouldn't normally instantiate the relationship by passing an object reference, ...
    (comp.object)
  • Re: specialized class template point of instantiation
    ... Thanks Tom! ... found only some of the members are instantiated. ... But anyway I would like the static member instantiated rather implicitly. ... > or you can explicitly instantiate it. ...
    (microsoft.public.vc.language)
  • Re: Global variables
    ... > each of the four client classes will be related to one or more members ... But that factory will also instantiate the the relationship to ... If EmailSender isn't created at startup, ... > EmailSender is created by a factory object, ...
    (comp.object)
  • Re: Class Placement - stupid question
    ... as far as you specify the proper namespace for it. ... Shouldn't I be able to instantiate classes anywhere in my event code? ... use their instance members. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: is this portable
    ... > using namespace std; ... > Is it save to get the address of the first member, ... There may be padding between the members and your ...
    (comp.lang.cpp)