Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用于从嵌套类型中提取模板参数的类型特征_C++_Templates_Typetraits - Fatal编程技术网

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
吗?一个很好很简单的解决方案。我尝试了很多不同的东西,但没有想到这个。谢谢!一个很好很简单的解决方案。我尝试了很多不同的东西,但没有想到这个。谢谢!