【发布时间】:2017-10-02 21:49:35
【问题描述】:
我想使用模板类型专门化一个函数,但我无法获得所需的结果。
考虑以下简单示例
#include <iostream>
#include <typeinfo>
#include <vector>
template <typename T>
void foo(){
std::cout << "In foo1 with type: " << typeid(T).name() << std::endl;
}
template< template<class, class...> class VEC, typename T>
void foo(){
std::cout << "In foo2 with vec type: " << typeid(VEC<T>).name()
<< " and template type: " << typeid(T).name() << std::endl;
}
int main() {
foo<int>();
foo<std::vector, int>();
foo<std::vector<int>>(); // Would like this to call the second version of foo
}
它的输出是
In foo1 with type: i
In foo2 with vec type: St6vectorIiSaIiEE and template type: i
In foo1 with type: St6vectorIiSaIiEE
有没有办法为 foo 的第二个版本编写模板签名,用于最后一次调用 foo(使用 std::vector 模板参数)?
谢谢!
【问题讨论】:
-
您想要:
template<> void foo<std::vector<int>>(){ std::cout << "In specialized foo1 with vec type: " << typeid(VEC<int>).name() << " and template type: " << typeid(int).name() << std::endl; }吗? -
@Jarod42 我想要更通用的东西