Bog*_*tza 2 c++ templates template-specialization
我正在尝试编写一个模板函数来处理任何类型的参数,包括矢量,地图等,但是遇到了麻烦。
template<class C>
void foo(C c);
template<>
template<class V>
void foo<std::vector<V> >(std::vector<V> v);
Run Code Online (Sandbox Code Playgroud)
编译器(g ++ 4.9.2-10)会抱怨:
test.cpp:13:43: error: too many template parameter lists in declaration of ‘void foo(std::vector<V>)’
void foo<std::vector<V> >(std::vector<V> v) {
^
test.cpp:13:6: error: template-id ‘foo<std::vector<V> >’ for ‘void foo(std::vector<V>)’ does not match any template declaration
void foo<std::vector<V> >(std::vector<V> v) {
^
test.cpp:13:43: note: saw 2 ‘template<>’, need 1 for specializing a member function template
void foo<std::vector<V> >(std::vector<V> v) {
^
Run Code Online (Sandbox Code Playgroud)
我该如何实现?我也想专门研究std :: map <std :: string,V>
从专业化中删除第一个template <>行仍然不起作用(非法专业化)
您不能部分专门化一个函数,而要创建一个新的重载:
template <class C>
void foo(C c);
template <class V>
void foo(std::vector<V> v); // This is a different overload, not a specialization!
Run Code Online (Sandbox Code Playgroud)
请注意部分专业化的区别foo:
template <class C>
void foo(C c); // 1
template <class V>
void foo<std::vector<V>>(std::vector<V> v); // 2
Run Code Online (Sandbox Code Playgroud)
这2是的部分专业化1,这在C ++中是不允许的。
| 归档时间: |
|
| 查看次数: |
1499 次 |
| 最近记录: |