Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String representation of a type

Reply
Thread Tools

String representation of a type

 
 
Scott Meyers
Guest
Posts: n/a
 
      01-29-2011
The draft C++0x standard tells me (in 7.1.6.2/4) the following:

const int&& foo();
int i;
decltype(foo()) x1 = i; // type is const int&&

Suppose I'd like to verify that decltype(foo()) really returns the type
const int&&. How can I do that? If I apply typeid and then print the
type's name,

std::string s = typeid(decltype(foo())).name();
std::cout << s << '\n';

VC10 prints "int" and gcc 4.5 prints "i". Both are standard-conforming,
because std::typeinfo::name can return whatever it wants, but neither
helps me confirm that decltype returns what the draft standard says it
should.

In their book, "C++ Template Metaprogramming," David Abrahams and
Aleksey Gurtovoy recommend the following approach, which they say will
cause a diagnostic to be issued showing the type:

template<typename T> struct incomplete;

incomplete<decltype(foo())> x;

This works as advertised, yielding

types.cpp(51) : error C2079: 'x' uses undefined class 'incomplete<T>'
with
[
T=const int &&
]

on VC10 and

types.cpp:51:33: error: aggregate 'incomplete<const int &&> x' has
incomplete type and cannot be defined

with gcc 4.5. But I'd really like to have a lexical representation of
the type in a std::string at runtime, so the compile-time-diagnostic
trick is not what I'm looking for.

Using type traits and/or overloading, I could write the code to generate
a string representation of a type, but dealing with all the
possibilities (e.g., function types, member pointer types, pointers to
pointers to pointers..., const and/or volatile qualifiers, etc.) would
be, er, time-consuming. I'm hoping that somebody has already consumed
that time and shared their work with the world. Is there some code
floating around somewhere that takes a type and returns a string
representation of that type, i.e., something that would let me write
something like

std::string s = Type<decltype(foo())>().name();
std::cout s; // prints something like "const int &&"

Thanks,

Scott
 
Reply With Quote
 
 
 
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      01-29-2011
Scott Meyers wrote:

> The draft C++0x standard tells me (in 7.1.6.2/4) the following:
>
> const int&& foo();
> int i;
> decltype(foo()) x1 = i; // type is const int&&
>
> Suppose I'd like to verify that decltype(foo()) really returns the type
> const int&&. How can I do that? If I apply typeid and then print the
> type's name,
>
> std::string s = typeid(decltype(foo())).name();
> std::cout << s << '\n';
>
> VC10 prints "int" and gcc 4.5 prints "i". Both are standard-conforming,
> because std::typeinfo::name can return whatever it wants, but neither
> helps me confirm that decltype returns what the draft standard says it
> should.
>
> In their book, "C++ Template Metaprogramming," David Abrahams and
> Aleksey Gurtovoy recommend the following approach, which they say will
> cause a diagnostic to be issued showing the type:
>
> template<typename T> struct incomplete;
>
> incomplete<decltype(foo())> x;
>
> This works as advertised, yielding
>
> types.cpp(51) : error C2079: 'x' uses undefined class 'incomplete<T>'
> with
> [
> T=const int &&
> ]
>
> on VC10 and
>
> types.cpp:51:33: error: aggregate 'incomplete<const int &&> x' has
> incomplete type and cannot be defined
>
> with gcc 4.5. But I'd really like to have a lexical representation of
> the type in a std::string at runtime, so the compile-time-diagnostic
> trick is not what I'm looking for.
>
> Using type traits and/or overloading, I could write the code to generate
> a string representation of a type, but dealing with all the
> possibilities (e.g., function types, member pointer types, pointers to
> pointers to pointers..., const and/or volatile qualifiers, etc.) would
> be, er, time-consuming. I'm hoping that somebody has already consumed
> that time and shared their work with the world. Is there some code
> floating around somewhere that takes a type and returns a string
> representation of that type, i.e., something that would let me write
> something like
>
> std::string s = Type<decltype(foo())>().name();
> std::cout s; // prints something like "const int &&"
>


Someone already did that. See
https://github.com/Eelis/geordi/blob...pe_strings.hpp . Help
is at http://www.xs4all.nl/~weegen/eelis/geordi/
 
Reply With Quote
 
 
 
 
Scott Meyers
Guest
Posts: n/a
 
      01-31-2011
On 1/28/2011 8:57 PM, Johannes Schaub (litb) wrote:
> Someone already did that. See
> https://github.com/Eelis/geordi/blob...pe_strings.hpp . Help
> is at http://www.xs4all.nl/~weegen/eelis/geordi/


I downloaded the source code and tried to find a way to use this
facility as part of a native C++ application, but it doesn't seem to be
designed to be used this way: it appears to be designed only to work on
Unix-like systems under gcc. Has anybody already ported this to
Windows? (gcc is fine, but it'd be nice if it ran under both gcc and
VC.) Or is there some other facility that will do what I want under
Windows?

Thanks,

Scott


 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      01-31-2011
Scott Meyers wrote:

> The draft C++0x standard tells me (in 7.1.6.2/4) the following:
>
> const int&& foo();
> int i;
> decltype(foo()) x1 = i; // type is const int&&
>
> Suppose I'd like to verify that decltype(foo()) really returns the type
> const int&&. How can I do that? If I apply typeid and then print the
> type's name,
>
> std::string s = typeid(decltype(foo())).name();
> std::cout << s << '\n';
>
> VC10 prints "int" and gcc 4.5 prints "i". Both are standard-conforming,
> because std::typeinfo::name can return whatever it wants, but neither
> helps me confirm that decltype returns what the draft standard says it
> should.


They seem to ignore cvref qualifiers on the main type, but:
template<class>struct Dummy{};
typeid(Dummy<decltype(...)>).name() contains the information, with a
little noise on the side. Same for typeid(void(decltype(...))).name()
I guess, or anything that moves the cvref inside the type.

> In their book, "C++ Template Metaprogramming," David Abrahams and
> Aleksey Gurtovoy recommend the following approach, which they say will
> cause a diagnostic to be issued showing the type:
>
> template<typename T> struct incomplete;
>
> incomplete<decltype(foo())> x;


Yes, and it gives you the demangling for free

> Using type traits and/or overloading, I could write the code to generate
> a string representation of a type, but dealing with all the
> possibilities (e.g., function types, member pointer types, pointers to
> pointers to pointers..., const and/or volatile qualifiers, etc.) would
> be, er, time-consuming.


Sounds like a nice "exercice for the reader"
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      01-31-2011
Marc wrote:

>> Using type traits and/or overloading, I could write the code to generate
>> a string representation of a type, but dealing with all the
>> possibilities (e.g., function types, member pointer types, pointers to
>> pointers to pointers..., const and/or volatile qualifiers, etc.) would
>> be, er, time-consuming.

>
> Sounds like a nice "exercice for the reader"


Like:
http://geometrica.saclay.inria.fr/te.../printtype.cpp
 
Reply With Quote
 
Scott Meyers
Guest
Posts: n/a
 
      02-01-2011
On 1/31/2011 3:35 PM, Marc wrote:
> Like:
> http://geometrica.saclay.inria.fr/te.../printtype.cpp


Very sweet! Did you just whip this up on the spur of the moment?

Scott


 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      02-01-2011
Scott Meyers wrote:

> On 1/31/2011 3:35 PM, Marc wrote:
>> Like:
>> http://geometrica.saclay.inria.fr/te.../printtype.cpp

>
> Very sweet! Did you just whip this up on the spur of the moment?


Yes, looked like fun. I probably missed a few corner cases though, and
the code could do with some cleanup...
 
Reply With Quote
 
Scott Meyers
Guest
Posts: n/a
 
      02-01-2011
On 2/1/2011 4:07 AM, Marc wrote:
> Yes, looked like fun. I probably missed a few corner cases though, and
> the code could do with some cleanup...


Still, very impressive. And thanks!

Scott


 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      02-02-2011
Marc <(E-Mail Removed)> writes:
>>> Like:
>>> http://geometrica.saclay.inria.fr/te.../printtype.cpp

>>
>> Very sweet! Did you just whip this up on the spur of the moment?

>
> Yes, looked like fun. I probably missed a few corner cases though, and
> the code could do with some cleanup...


Of course it depends on the system's type_info::name method to return
the names of class types, which it doesn't in gcc; in gcc,
type_info::name returns the mangled type name, so your proggie yields:

5Perso
unsigned long* const* volatile*&()
void(*&)(int(*)(),5Perso&)
5Perso const&(...)
long double(int volatile*,...)
int(5Perso::**)(double)
short const volatile 5Perso::*
int(*(*(&)(long))(char))(double)

I guess that could be worked-around by just skipping any digits at the
beginning of the class name, which obviously aren't a valid part of the
name...

-Miles

--
/\ /\
(^.^)
(")")
*This is the cute kitty virus, please copy this into your sig so it can spread.
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      02-02-2011
Miles Bader wrote:

> Marc <(E-Mail Removed)> writes:
>>>> http://geometrica.saclay.inria.fr/te.../printtype.cpp

>
> Of course it depends on the system's type_info::name method to return
> the names of class types, which it doesn't in gcc; in gcc,
> type_info::name returns the mangled type name, so your proggie yields:


Well yes, I don't think there is much that can be done about it, so I
used type_info::name as a default. Note that you could still register
your own classes, templates, etc, but that's a not as nice.

> I guess that could be worked-around by just skipping any digits at the
> beginning of the class name, which obviously aren't a valid part of the
> name...


That might work for simple classes, but as soon as you add templates
to the mix... And you can't just stick _Z in front and call c++filt to
demangle, because sometimes you need a couple more letters in between.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot create an object of type 'CustomWizard' from its string representation... Allan Ebdrup ASP .Net 1 07-16-2007 12:06 PM
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Cannot create an object of type 'System.String[]' from its representation 'String[] Array' Hessam ASP .Net Building Controls 1 08-16-2003 10:26 AM
Cannot create an object of type 'System.String[]' from its representation 'String[] Array' Hessam ASP .Net Web Controls 0 08-08-2003 08:36 AM
Cannot create an object of type 'System.String[]' from its representation 'String[] Array' Hessam ASP .Net 0 08-08-2003 08:36 AM



Advertisments