C++ 用于从嵌套类型中提取模板参数的类型特征
我需要从嵌套类型获取模板参数。下面是一个简单的示例来显示我需要提取的类型C++ 用于从嵌套类型中提取模板参数的类型特征,c++,templates,typetraits,C++,Templates,Typetraits,我需要从嵌套类型获取模板参数。下面是一个简单的示例来显示我需要提取的类型 #include <iostream> #include <typeinfo> template<typename T> void function(T) { // T = 'struct A<int>::B' // // Here I want to get the template value type e.g. 'int' from T
#include <iostream>
#include <typeinfo>
template<typename T>
void function(T) {
// T = 'struct A<int>::B'
//
// Here I want to get the template value type e.g. 'int' from T
// so that this would print 'int'. How can this be done?
std::cout << typeid(T).name() << std::endl;
}
template<typename T>
struct A {
using B = struct { int f; };
};
int main() {
function(A<int>::B{});
return 0;
}
#包括
#包括
模板
空洞函数(T){
//T='结构A::B'
//
//这里我想从T中获取模板值类型,例如“int”
//这样就可以打印“int”了。怎么做呢?
std::cout你不能通过简单的推导来提取它。虽然B
是a
的嵌套类,但类型本身是不相关的
一个选项是在B
中“保存”类型,然后将其解压缩:
template<typename T>
struct A {
struct B {
using outer = T;
int f;
};
};
虽然B
是a
的嵌套类,但类型本身是不相关的
一个选项是在B
中“保存”类型,然后将其解压缩:
template<typename T>
struct A {
struct B {
using outer = T;
int f;
};
};
你能在B
里面添加typedef
吗?你能在B
里面添加typedef
吗?一个很好很简单的解决方案。我尝试了很多不同的东西,但没有想到这个。谢谢!一个很好很简单的解决方案。我尝试了很多不同的东西,但没有想到这个。谢谢!