Re: specialized class template point of instantiation
From: Tom Widmer (tom_usenet_at_hotmail.com)
Date: 02/24/05
- Next message: jpuopolo_at_mvisiontechnology.com: "Re: ASM Question"
- Previous message: Tom Widmer: "Re: Assertion error when using Free (non-local pointer)"
- In reply to: ben: "specialized class template point of instantiation"
- Next in thread: ben: "Re: specialized class template point of instantiation"
- Reply: ben: "Re: specialized class template point of instantiation"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: jpuopolo_at_mvisiontechnology.com: "Re: ASM Question"
- Previous message: Tom Widmer: "Re: Assertion error when using Free (non-local pointer)"
- In reply to: ben: "specialized class template point of instantiation"
- Next in thread: ben: "Re: specialized class template point of instantiation"
- Reply: ben: "Re: specialized class template point of instantiation"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|