www.digitalmars.com         C & C++   DMDScript  

c++ - template class typeid(&).name() has an extra space

reply Richard <fractal clark.net> writes:
This seemed so minor, but you never know:

if you do this

string A = typeid(*this).name();

inside a template, you will get an extra space after the typename..

#include <iostream>
#include <typeinfo.h>

struct Root { };
template<typename T> struct Test {
Test() { cout << typeid(*this).name() << endl; }
};

void main() {
Test<Root> t;
}

// output is: Test<Root >

I verified that it is a space.

Richard
Dec 04 2002
parent Larry Brasfield <larry_brasfield snotmail.com> writes:
In article <asln3j$5tb$1 digitaldaemon.com>, 
Richard (fractal clark.net) says...
 This seemed so minor, but you never know:
 
 if you do this
 
 string A = typeid(*this).name();
 
 inside a template, you will get an extra space after the typename..
 
 #include <iostream>
 #include <typeinfo.h>
 
 struct Root { };
 template<typename T> struct Test {
 Test() { cout << typeid(*this).name() << endl; }
 };
 
 void main() {
 Test<Root> t;
 }
 
 // output is: Test<Root >
 
 I verified that it is a space.

I don't think it's a bug. The actual text that shows up from a type_info.name() is not specified by the C++ standard. I suspect that the trailing space on templated names is useful for the case where templates are nested, making the result more like what you have to type in those cases. For example, you might have std::vector<std::list<MyType> > whereas it would be syntactically wrong to have std::vector<std::list<MyType>> Of course, the ultimate trailing space is not necessary, but if it helps in the composition of templated names, it is harmless and legal. -- -Larry Brasfield (address munged, s/sn/h/ to reply)
Dec 04 2002