c++ Human readable type names on both platforms

Exact types of Fasteners

Update 2021 Aug

Design decision explanation. To use this API one has to provide the type.

That does not mean users are not able to use type instances or variables:

That 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++ which 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 is this:

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

Before we Begin

By 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 dependant 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.

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. (Isn’t it funny how quickly we forgot both Wandbox and Coliru?)

2018 May 24

In case you like to experiment or you 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 do copy-paste.

Leave a Reply

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

%d bloggers like this: