C++ Human readable type names on both platforms

Exact types of Fasteners

Update 2022 Nov

Godbolt is here: https://godbolt.org/z/h6YK91d54

The code is much improved. The old bad version remains below to shame the author. Usage remains simple

That code is C++. On windows I use clang.

Completely different and working approach: https://stackoverflow.com/a/56766138 . Not mine.

Update 2021 Aug

Design decisions explanations. To use this API one has to provide the type.

That does not mean one can not use instances or variables:

Beware! Here is a deliberate warning for the user.  Objects and variables mean instantiation. Default constructors mandate and all that jazz.  By sticking to types we stay in the realm of type computing. In C++ that also enables metaprogramming. Although not right now. That is because not before C++23 (hopefully) many more “things” will be constexpr enabled, and we would be able to declare it as this:

Also, all the functions used there will have to be constexpr enabled.

Before we Begin

By the folly of much hacking, one could make it compile-time right now, but that is a path peppered with shards of glass. If you might disagree please submit your code.

There is this much upvoted C++ hack I know about on Stack Overflow.  There are even more complex “tricks” too. Be forewarned: every trick is highly dependent on things that might change at random, in every new release of any of the compilers used. Thus no trick is advised, by me. Now back to safe seas.

Update 2021 Apr

Modern times. Godbolt and the proper immediately obvious test online of our boring simple and resilient solution. Here is the full code in case you can not be bothered to follow that Godbolt link. Please read the rest of the prose below. (Isn’t it funny how quickly we forgot both Wandbox and Coliru?)

Note the optimization vs the previous version. Since one type has one name for the lifetime of a program above we do that only once and not before requested.

2018 May 24

In case you like to experiment or 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 a headache, while you are trying to understand SFINAE, no less.

And very likely you are using Stack Overflow to find answers. And over there you realize not all 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 widespread 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 the  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.

You will need to write a little code (like the one above ) to check this or that, using one of the good online 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, runtime, and platform.

Here it is

The usage is explained in the code. And here:

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 copy-paste.