'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.
Solution 1:[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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
