Re: specialized class template point of instantiation

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

From: ben (benhongh_at_hotmail.com)
Date: 02/24/05


Date: Fri, 25 Feb 2005 10:02:03 +1100

Thanks Tom! Now it really makes sense. I have been testing the code and
found only some of the members are instantiated.

But anyway I would like the static member instantiated rather implicitly. I
don'w want to declare objects (notice the private constructor), neither do I
want to use macros. What are my options? viz to tell the compiler not to
optimize off the static definition much the same way a "volatile" keyword
does to unused variables.

> 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
    ... > using namespace std; ... implicit instantiation. ... but simply instantiating the class template *won't* instantiate its members. ...
    (microsoft.public.vc.language)
  • Re: Reason for use of :: notation in static access?
    ... is already used to access type object members, ... I've never liked the new static member access operator. ... simplify the lives of developers was to make access to type members always ... mistake because it makes the MSH language that much harder. ...
    (microsoft.public.windows.server.scripting)
  • Re: Re: Opinions on the Law Of Demeter
    ... in one case when factory = CFooFactory....then it defininately ... that you only instantiate when you actually are going to use it. ... >> Are you contending that my CLazyProduct class always creates a new ... >> static member, would violate the Law of Demeter? ...
    (comp.object)
  • Re: Private constructor
    ... A static member function ... >> has access to the private members of other objects of the same class ... >> members of other instances of the class. ...
    (comp.lang.cpp)