0

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.

paolo
  • 2,345
  • 1
  • 3
  • 17
  • The `T` in the MSVC error message is the `T` in the `f` template, not the `T` in the `TD` template. Would be clearer if you renamed one of the template arguments. This is what it has always been. – Some programmer dude Apr 29 '22 at 09:22
  • I've updated my question and changed `T` in `TD` to `U` so that maybe it's a bit clearer. However, VS would still tell me that the `T` I (try) instanciate `TD` with is actually an `int`, which is what I want to know. – paolo Apr 29 '22 at 09:31
  • 2
    My trick is `template void whatis();` followed by `whatis();` produces "unresolved reference to void whatis(void)". – Raymond Chen May 02 '22 at 02:32
  • Thanks, @Raymond. Moving the error from compile to link time is certainly a good solution (please consider posting your comment as an answer). Do you know if there's a compile-time error I could trigger that would produce the information I need? – paolo May 02 '22 at 07:29

1 Answers1

1

You can use typeid (The typeid operator allows the type of an object to be determined at run time). Try the snippet below:

#include <iostream> 
#include <string> 
#include <typeinfo> 
using namespace std;
template <typename U>
struct TD {};

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

}

int main()
{
    f(1);
}

Output:

struct TD<unsigned int>
Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14
  • Thanks for your answer but this doesn't work as `typeid` will ignore references. Here's an example from the bottom of the page you linked: `typeid(int) == typeid(int&)` (evaluates to true). – paolo May 02 '22 at 07:07