vendredi 29 avril 2022

Get name of type behind type alias in function template

I'm working with Visual Studio 2013.

When I need to know what types the compiler deduced for my template parameters, I usually trigger a compiler error like this:

template <typename U>
struct TD;

template <typename T>
void f(T) {
    TD<T> t{};
}

int main() {
    f(1);
}

And this works, as Visual Studio will tell me I tried to instanciate my undefined struct TD with an int:

error C2079: 't' uses undefined struct 'TD<T>'
        with
        [
            T=int
        ]
note: see reference to function template instantiation 'void f<int>(T)' being compiled
        with
        [
            T=int
        ]

However, the above trick becomes useless on Visual Studio if I want to know what's the type behind a type alias. E.g. the following:

template <typename T>
void f(T) {
    using unsigned_int_t = typename std::make_unsigned<T>::type;
    TD<unsigned_int_t> t{};
}

will produce:

error C2079: 't' uses undefined struct 'TD<unsigned_int_t>'
note: see reference to function template instantiation 'void f<int>(T)' being compiled
        with
        [
            T=int
        ]

Whereas GCC will tell me that unsigned_int_t is a unsigned int:

error: 'TD<unsigned int> t' has incomplete type
   10 |     TD<unsigned_int_t> t{};

So, is there a way to get the name of the type behind a type alias with Visual Studio 2013?

Note: I already had a look at Print template typename at compile time but no answer worked for me.

Aucun commentaire:

Enregistrer un commentaire