Search code examples
c++typedefincomplete-type

Is it possible to expand typedef with forced compiler error?


I have been using method shown below to force compiler to yell at me a variable type:

template <class T>
struct show_type;

Using it with desired variable so compiler errors an incomplete struct type:

typedef int32_t s32;
s32 a;
show_type<decltype(a)>();

So GCC 5.3.0 produces error:

invalid use of incomplete type 'struct show_type<int>'

And MSVC 2015:

'show_type<s32>': no appropriate default constructor available

Now I wonder if there is a way to force an error to show full hierarchy of typedefs (that is, s32 -> int32_t -> int), or at least newest typedef and first original type? I don't mind dirty or evil tricks.


Solution

  • Now I wonder if there is a way to force an error to show full hierarchy of typedefs (that is, s32 -> int32_t -> int), or at least newest typedef and first original type?

    There is no such hierarchy. s32 is int32_t is int. There is no way to differentiate those three types, since they aren't actually three different types. Two of those are just aliases.


    What you're really looking for is static reflection, or P0194. That would allow you to do something like:

    using meta_s32   = reflexpr(s32);
    using meta_int32 = meta::get_aliased_t<meta_s32>;
    using meta_int   = meta::get_aliased_t<meta_int32>;
    std::cout << meta::get_name_v<meta_s32> << ", "
              << meta::get_name_v<meta_int32> << ", "
              << meta::get_name_v<meta_int> << '\n';
    

    You could produce the reflection hierarchy by repeatedly going up with get_aliased_t and stopping when is_alias_v yields false_type.