c++ : human readable type names on both platforms

In case you like to experiment and learn modern C++, you will be needing some R&D little nuggets of your own.

For example. C++ is notorious for its powerful meta programming capabilities.  Thus it is very likely, in your C++ quest, you need to see the name of some type that is giving you headache.

And very likely you are using Stack Overflow to find answers. And over there you realize not all the people use your beloved Visual Studio and MSVC. A lot of good C++ programmers/architects do use gcc and clang.  After all clang is in wide spread usage in some core C++ teams inside Microsoft too.

So. The little problem in that clang/gcc code is that type names  do stay mangled by default.

As an example do the “Run code” here. That is the page on typeid() operator, on cppreference.com.

Meaning your MSVC code where you need to see the type name will produce mangled names on those numerous clang/gcc samples.

// some wandbox, coliru, etc. code
int main ( int argc, char * argv[] ) {

auto mangled_in_clang_gcc =
     typeid( decltype( argv ) ).name() ;

You will need to write little code (like the one above ) to check this or that, using one of the good on-line compilers like the Wandbox I am using.

Not to worry. I have written a little function for you, to use when you need to see human readable names in either MSVC on Windows , or clang and gcc on Linux as is the Wandbox or Coliru, run time and platform.

Here it is:

The usage is explained in the code. And here:

int main ( int argc, char * argv[] ) {
auto human_readable_type_name =
     dbj::demang< decltype( argv ) >() ;

I do prefer Wandbox also because one can add headers, so I just add the code as a header. For Coliru you have only one file (AFAIK?) so you will have to do copy paste.

So little C++ so much good!
So little C++, so much good!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.