5

I am newly studying auto feature of C++11/14.

For educational purpose, I would like to explicitly display the result of type inference of my code. I tried typeid().name(), but I found two problems with this approach.

  1. Output is sometimes difficult to understand. (For example, "NSt3__16vectorIiNS_9allocatorIiEEEE")
  2. const/volatile modifiers do not seem to be displayed.

@πάνταῥεῖ I have tried using abi::__cxa_demangle() you pointed out. Problem 1 is solved, thank you, but typeid().name() does not seem to contain CV modifier information. I think there are some pitfalls using auto keyword, so I would like to see the exact result of the type inference, including CV modifier and reference type.

I am using clang 6.1.0 on mac os 10.10.3, but I would like to know portable way to do this, if possible.

tuun
  • 51
  • 3
  • @JBL It doesn't need to be the same question, as long there's an appropriate answer, and there is IMHO. – πάντα ῥεῖ Apr 28 '15 at 10:24
  • @πάνταῥεῖ Well, the most practical answer to this isn't on the duplicate, and wouldn't really fit imho, but well... (Though it's true there are answers that can help him) – JBL Apr 28 '15 at 10:26
  • @JBL Well, I'm going to reopen the question, that you can post a better answer. – πάντα ῥεῖ Apr 28 '15 at 10:28
  • 1
    What compiler and OS are you using? – Abhijit Apr 28 '15 at 10:35
  • Is [this](http://coliru.stacked-crooked.com/a/23efe33da4bd804f) and [this](http://coliru.stacked-crooked.com/a/0790bc62fb7e3434) what you mean ? – Piotr Skotnicki Apr 28 '15 at 10:45
  • I was asking nearly the same a while ago... check out [my question](http://stackoverflow.com/questions/25891266/template-which-prints-the-name-of-the-given-type) and its answers, thew could be useful for you. – PaperBirdMaster Apr 28 '15 at 11:22

2 Answers2

7

Try the approach proposed by Scott Meyers (Effective Modern C++):

Declare a template (but don't define it)

template<typename T>       // declaration only for TD;
class TD;                  // TD == "Type Displayer"

Then instantiate this template using your type

TD<decltype(x)> xType

The compiler will now complain about this incomplete type (and usually will display the full name of it)

error: aggregate 'TD< int > xType' has incomplete type and cannot be defined

See Item 4 of "Effective Modern C++" (generally I'd propose this book as a "must read")

Daniel
  • 1,041
  • 7
  • 13
  • Ah damn, that was the same idea! :D – JBL Apr 28 '15 at 10:50
  • @JBL Not quite the same. `decltype(x)` may be a reference type, while `T` in your code will never be a reference type. – Lingxi Apr 28 '15 at 10:51
  • @JBL: As I said: a must read... So not astonishing that other people have read it too... – Daniel Apr 28 '15 at 10:51
  • Ah right, did not catch that.. But is using `decltype` the only way then? – JBL Apr 28 '15 at 10:52
  • Thank you very much for quick reply! Actually I am beginning to read "Effective Modern C++" and I wanted to check the actual behavior of Item 1... – tuun Apr 28 '15 at 10:53
  • @JBL: probably not (Out of experience there almost never is "Only that way"). However since the approach is pretty simple: Why continue searching? – Daniel Apr 28 '15 at 10:56
  • That was just by pure curiosity. – JBL Apr 28 '15 at 10:57
  • @JBL: The only way that is even easier is if you have an IDE that can show you the type directly: Hover over the variable/expression you need to check: There you have the type... (But then you need an IDE which is capable of this) – Daniel Apr 28 '15 at 10:57
  • @JBL: One approach (not sure if it works) would also be to use your algorithm with a "perfect forwarding" parameter (e.g. T&& param) – Daniel Apr 28 '15 at 10:59
5

Type Index library was recently added to Boost. It tries to address some of the problems you metioned.

Example:

cout << boost::typeindex::type_id<int const volatile*>().pretty_name() << endl;
cout << boost::typeindex::type_id_with_cvr<int const&>().pretty_name() << endl;

Prints:

int const volatile*
int const&
Johny
  • 1,947
  • 13
  • 23