我正在尝试执行以下操作:
#include <iostream>
#include <vector>
#include <tuple>
#include <list>
template <typename T>
void f(T t) {
std::cout << "1" << std::endl;
}
template <typename T, typename V>
void f(T<std::tuple<V>> t) {
std::cout << "2" << std::endl;
}
int main() {
f(std::list<double>{}); // should use first template
f(std::vector<std::tuple<int>>{}); // should use second template
}
在 C++14 中执行此操作的最简单方法是什么?我认为我可以通过这种方式进行模式匹配,但编译器不会有它。
模板参数T
用作模板名称,因此应将其声明为模板 模板参数 http://en.cppreference.com/w/cpp/language/template_parameters#Template_template_parameter. e.g.
template <template <typename...> class T, typename V>
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void f(T<std::tuple<V>> t) {
std::cout << "2" << std::endl;
}
LIVE http://rextester.com/JXC67262
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)