Type as String Library

I have need of a library that converts a compile time type to a string. For example: namespace testNamespace { struct aStruct{}; } void main() { std::cout << TypeString<int>::str() << std::endl; std::cout << TypeString<aStruct>::str() << std::endl; } would output: int testNamespace::aStruct I have been playing around with code that does this, but I wanted to make sure I wasn't duplicating any work that someone else has already done. I looked through the boost sandbox, but didn't find anything. Am I missing something? If not, is this something that there would be interested in having? Clearly it would be compiler dependent but I know I can get it to work on Visual Studio and I think I could do it with gcc. Jared

Jared McIntyre wrote:
I have need of a library that converts a compile time type to a string.
<example deleted>
I have been playing around with code that does this, but I wanted to make sure I wasn't duplicating any work that someone else has already done. I looked through the boost sandbox, but didn't find anything. Am I missing something? If not, is this something that there would be interested in having? Clearly it would be compiler dependent but I know I can get it to work on Visual Studio and I think I could do it with gcc.
Jared
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Such code must exist within compilers, as the error output is full of such information. It would sometimes be nice to get the compiler to say what is happening when there is not an error. This idea would be something similar. John Fletcher

Check out boost::typeof, otherwise you have typeid(T).name() or gcc's typeof extension. Philippe

Philippe Vaucher wrote:
Check out boost::typeof, otherwise you have typeid(T).name() or gcc's typeof extension.
I'd guess the OP then also wants some (compiler-specific) demangler functionality to map back to human-readable type names. That may or may not be available in existing APIs, depending on the compiler / runtime being used. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...

Philippe Vaucher <philippe.vaucher <at> gmail.com> writes:
Check out boost::typeof, otherwise you have typeid(T).name() or gcc's typeof extension.
Philippe
I wasn't aware of the name function in typeid, but I figured there had to be something out there already. Is the format of the output string specified in the standard? i.e can I trust that it will be the same from compiler to compiler in order to use it in cross-platform code? Jared

Jared McIntyre wrote:
I wasn't aware of the name function in typeid, but I figured there had to be something out there already. Is the format of the output string specified in the standard? i.e can I trust that it will be the same from compiler to compiler in order to use it in cross-platform code?
No. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...

Is the format of the output string specified in the standard? i.e can I trust that it will be the same from compiler to compiler in order to use it in cross-platform code?
No, but I know gcc gives you a demangle function so you can get a proper name out of it. If you're really interested I can try to find it. Philippe

Philippe Vaucher wrote:
Is the format of the output string specified in the standard? i.e can I trust that it will be the same from compiler to compiler in order to use it in cross-platform code?
No, but I know gcc gives you a demangle function so you can get a proper name out of it. If you're really interested I can try to find it.
Just look into the implementation of boost.python. Gcc's demangler is used there to produce convenient error / exception text. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...

Stefan Seefeld wrote:
Philippe Vaucher wrote:
Is the format of the output string specified in the standard? i.e can I trust that it will be the same from compiler to compiler in order to use it in cross-platform code?
No, but I know gcc gives you a demangle function so you can get a proper name out of it. If you're really interested I can try to find it.
Just look into the implementation of boost.python. Gcc's demangler is used there to produce convenient error / exception text.
Note also that it's not just a compiler to compiler difference in the typeid names. They differ from one compiler version to the next compiler version. So if you want consistent names, you either create them yourself, or use demangling utilities. -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

Rene Rivera wrote:
Stefan Seefeld wrote:
Philippe Vaucher wrote:
Is the format of the output string specified in the standard? i.e can I trust that it will be the same from compiler to compiler in order to use it in cross-platform code?
No, but I know gcc gives you a demangle function so you can get a proper name out of it. If you're really interested I can try to find it. Just look into the implementation of boost.python. Gcc's demangler is used there to produce convenient error / exception text.
Note also that it's not just a compiler to compiler difference in the typeid names. They differ from one compiler version to the next compiler version. So if you want consistent names, you either create them yourself, or use demangling utilities.
Well, it isn't quite as bad as you make it sound. At least now we have a standard ABI that name mangling is part of: http://www.codesourcery.com/cxx-abi/abi.html Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...

Note also that it's not just a compiler to compiler difference in the typeid names. They differ from one compiler version to the next compiler version. So if you want consistent names, you either create them yourself, or use demangling utilities.
My original prototype was demangling the values in an attempt to make them standard. I was using a different method than typeid().name(), but it looked like either method could work, with typeid().name() being the better method. It was just going to be a lot of compiler specific code. I've since found a different way to solve my problem that doesn't use string descriptions of the type, but I do think it would be nice to have a library that will give you the same string across supported compilers (at least wherever possible). Jared

On 4/26/07, Jared McIntyre <jmcintyre@dfsoftware.com> wrote: <snip> My original prototype was demangling the values in an attempt to make them
standard. I was using a different method than typeid().name(), but it looked like either method could work, with typeid().name() being the better method. It was just going to be a lot of compiler specific code. I've since found a different way to solve my problem that doesn't use string descriptions of the type, but I do think it would be nice to have a library that will give you the same string across supported compilers (at least wherever possible).
Jared
<snip> I wonder if your code is general enough to handle classes/structs defined inside several layers of namespaces, and/or nested classes/structs defined inside other classes/structs (could be several layers down as well). Also, do it handle class templates as well? -- Greg
participants (6)
-
Gregory Dai
-
Jared McIntyre
-
John Fletcher
-
Philippe Vaucher
-
Rene Rivera
-
Stefan Seefeld