This is a multi-part message in MIME format.
------=_NextPart_000_0019_01C51AB5.A1F0E910
Content-Type: text/plain;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
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; =20
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;
};
template <typename T>
MyTypeInfo TypeInfoBase<T>::TI =3D 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>{};
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??
return 0;
}
------=_NextPart_000_0019_01C51AB5.A1F0E910
Content-Type: text/html;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2800.1106" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV> </DIV>
<DIV><FONT face=3DArial>In the following program, why won't =
member <U>static=20
MyTypeInfo TypeInfoBase<A>::TI</U> be instantiated?</FONT></DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV><FONT face=3DArial>Ben</FONT></DIV>
<DIV><FONT face=3D"Courier New" color=3D#008000 =
size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" color=3D#008000=20
size=3D2>/////////////////////////////////</FONT></DIV>
<DIV><FONT size=3D2></FONT><FONT size=3D2></FONT><FONT =
size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2><FONT =
color=3D#0000ff>#include</FONT>=20
<list></FONT><BR><FONT size=3D2><FONT face=3D"Courier New"><FONT=20
color=3D#0000ff>#include</FONT> <iostream><BR><FONT=20
color=3D#0000ff>#include</FONT> <typeinfo></FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"><FONT =
color=3D#0000ff>using=20
namespace</FONT> std;</FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>list<MyTypeInfo&>=20
ListOfTypes;</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"><FONT =
color=3D#0000ff>struct</FONT>=20
MyTypeInfo <FONT color=3D#008000>// purpose: to record a=20
type</FONT><BR>{<BR> type_info&=20
tid; </FONT></FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"> =20
MyTypeInfo(type_info& ti)</FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"> =20
:tid(ti)<BR> {<BR></FONT></FONT><FONT size=3D2><FONT=20
face=3D"Courier New"> =20
ListOfTypes.push_back(*this);</FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"> =
=20
cout << ti.name();<BR> =
}<BR>};</FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"><FONT =
color=3D#0000ff>template</FONT>=20
<<FONT color=3D#0000ff>typename</FONT> T><BR> =
<FONT=20
color=3D#0000ff>class</FONT> TypeInfoBase<BR> =20
{<BR> <FONT=20
color=3D#0000ff>private</FONT>:<BR> =
TypeInfoBase();</FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"> <FONT=20
color=3D#0000ff>public</FONT>:<BR> =20
<STRONG><FONT color=3D#0000ff>static</FONT> <FONT =
color=3D#0000ff>typename</FONT>=20
MyTypeInfo TI;</STRONG></FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> =
};</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"><STRONG><FONT=20
color=3D#0000ff>template</FONT> <<FONT =
color=3D#0000ff>typename</FONT>=20
T><BR></STRONG> <STRONG>MyTypeInfo=20
TypeInfoBase<T>::TI =3D MyTypeInfo(<FONT=20
color=3D#0000ff>typeid</FONT>(T));</STRONG></FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"><FONT =
color=3D#0000ff>template</FONT>=20
<<FONT color=3D#0000ff>typename</FONT> T></FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2> <FONT=20
color=3D#0000ff>class</FONT> TypeTrait{};</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"><FONT =
color=3D#0000ff>class</FONT>=20
A</FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>{</FONT></DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"> <FONT=20
color=3D#008000>//...</FONT></FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>};</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" color=3D#008000 size=3D2>// specialize=20
TypeTrait<A> to construct the static object</FONT></DIV>
<DIV><FONT face=3D"Courier New"><STRONG><FONT size=3D2><FONT=20
color=3D#0000ff>template</FONT> <> </FONT><FONT size=3D2><FONT=20
color=3D#0000ff>class</FONT> TypeTrait<A>: <FONT =
color=3D#0000ff>public</FONT>=20
TypeInfoBase<A>{</FONT><FONT =
size=3D2>};</FONT></STRONG></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New"><FONT =
color=3D#0000ff>int</FONT>=20
main(<FONT color=3D#0000ff>int</FONT> argc, <FONT =
color=3D#0000ff>char</FONT>*=20
argv[])<BR>{</FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" color=3D#008000 =
size=3D2> //=20
TypeInfoBase<A> should have been instantiated at this =
point</FONT></DIV>
<DIV><FONT face=3D"Courier New" color=3D#008000 =
size=3D2> // and so=20
should TypeInfoBase<A>::TI be constructed.</FONT></DIV>
<DIV><FONT face=3D"Courier New" color=3D#008000 =
size=3D2> // But it=20
is not! Why??</FONT></DIV>
<DIV><FONT size=3D2><BR><FONT face=3D"Courier New"> =
<FONT=20
color=3D#0000ff>return</FONT> 0;<BR>}</FONT></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" =
size=3D2></FONT> </DIV></BODY></HTML>
------=_NextPart_000_0019_01C51AB5.A1F0E910--