www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

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

↑ ↓ ← 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
↑ ↓ → 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