Re: specialized class template point of instantiation
From: ben (benhongh_at_hotmail.com)
Date: 02/24/05
- Next message: Doug Harrison [MVP]: "Re: Explicitly calling constructors/destructors"
- Previous message: Dave: "Problem closing handle"
- In reply to: Tom Widmer: "Re: specialized class template point of instantiation"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Doug Harrison [MVP]: "Re: Explicitly calling constructors/destructors"
- Previous message: Dave: "Problem closing handle"
- In reply to: Tom Widmer: "Re: specialized class template point of instantiation"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|