Distinguish between an alias and real types at compile time?

I wrote a template function that takes an arbitrary number of types and displays their sizes for the underlying architecture and operating system. However, a function cannot distinguish an alias from a real type, so it is evaluated as if it is real.

However, I want to be able to distinguish between an alias and an inline type at compile time and alternate the output based on this.

func<unsigned int, size_t>(); 

Output:

 Unsigned int is 4 bytes. Unsigned int is 4 bytes. 

However, I want the result to be so

 Unsigned int is 4 bytes. size_t is an alias for unsigned int. 

Of course, this requires that the compiler can distinguish between an alias and an inline type at compile time.

So, is there a way to differentiate the real type and the alias at compile time in any version of C ++?

+6
source share
2 answers

The answer is that you cannot do it now . However, there is a suggestion for static reflection: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0194r2.html

This document mentions Operation get_base_name , which will return the type name. However, they state:

The get_base_name called in meta :: Alias ​​returns the name of the alias, not the name of the declaration with the alias.

They then provide Operation get_aliased , which can be used to get the original alias type when used with get_base_name .

Example code from a document:

  using rank_t = int; using mR = reflexpr(rank_t); cout << "5:" << get_base_name_v<mR> << endl; cout << "6:" << get_base_name_v<get_aliased_m<mR>> << endl; 

outputs the following result:

  5:rank_t; 6:int; 

Bonus: if you have an interest to try it now, the next document http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0385r1.pdf mentions that there is an initial experimental implementation on a quiz on github here: https://github.com/matus-chochlik/clang/tree/reflexpr .

+6
source

You were unlucky.

Unfortunately, there is no way to allocate at compile time or runtime if the type is a primitive type or a typedef of a primitive type.

+6
source

Source: https://habr.com/ru/post/1013950/


All Articles