检索递归可变参数模板的特殊类型参数

时间:2017-02-22 23:14:53

标签: c++ c++11 templates variadic-templates template-specialization

这样的事情是可能的:

#include <iostream>
#include <string>

template<typename T1, typename T2, typename T3, typename T4, class ...Ts>
struct third_t {

    typedef typename third_t<T2,T3,T4,Ts...>::type type;
};

template<typename T1,typename T2, typename T3>
struct third_t<T1,T2,T3,void> {

    typedef T3 type;
};

int main() {

    third_t<int,int,std::string,unsigned>::type z = "test";

    std::cout<<"string type: "<<z<<std::endl;
}

如果是,为什么不找到专门案例?即它甚至没有在决议中考虑过。

我收到错误:

main.cpp: In instantiation of 'struct third_t<int, int, std::__cxx11::basic_string<char>, unsigned int>':

main.cpp:18:46:   required from here

main.cpp:7:56: error: wrong number of template arguments (3, should be at least 4)

     typedef typename third_t<T2,T3,T4,Ts...>::type type;

更新

我意识到我最初的错误推定使我认为我可以“超载”一个班级,这当然是无稽之谈。虽然可以使用功能模板完成类似的事情,但

template<typename T1,typename T2, typename T3>
struct third_t { ... };

无法使用类模板完成,如果是这样的话,就可以从模板参数中提取第三个 last 参数。

0 个答案:

没有答案
相关问题