Scott Meyers
Usenet Archive
- Joined
- Oct 31, 2006
- Messages
- 109
- Reaction score
- 0
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
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