当使用Tempate作为参数时,功能模板专业化
The function template specialization when using tempates as argument
我想编写一个函数模板来处理向量,列表,sets,...并想编写专业功能以分别处理地图,当我编写以下代码并报告错误时。
有人可以帮助我如何修改它吗?
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
using namespace std;
// test() for the vectors, lists, sets, ...
template <template <typename...> class T>
void test()
{
T<string, int> x;
//...
}
// specialize test() for map
template <>
void test <map<> class T>()
{
T<string, int> x;
//...
}
int main()
{
test<map>();
test<unordered_map>();
}
模板专业应为:
template <>
void test <std::map>()
{
std::map<string, int> x;
}
我将模板参数从map<> class T
更改为std::map
无效的语法。我将T<string, int>
更改为std::map<string, int>
,因为该名称T
在专业中不存在。
以下是有效的完整代码:
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
using namespace std;
template <template <typename...> class T, typename TN>
void test()
{
cout << "---------- test case 1" << endl;
T<TN, int> x;
}
template <template <typename...> class T>
void test()
{
cout << "---------- test case 2" << endl;
T<string, int> x;
}
template <>
void test < map,string >()
{
cout << "---------- test case 3" << endl;
map<string, int> x;
}
template <>
void test < map >()
{
cout << "---------- test case 4" << endl;
map<string, int> x;
}
int main()
{
test<unordered_map,string>(); // trigging test case 1
test<unordered_map>(); // trigging test case 2
test<map,string>(); // trigging test case 3
test<map>(); // trigging test case 4
}
相关文章:
- 类模板的成员功能的定义在单独的TU中完全专业化
- 特定好友功能专业化
- 如何部分专业化功能以用元组元素作为参数调用功能
- 功能模板参数包,然后是模板参数和专业化
- 不允许功能模板的部分专业化背后的理由是什么?
- 在此功能[ - wuninitialization]中使用非专业化
- 模板专业化的情况下,如果有变态模板功能
- 如何在另一个名称空间内从另一个命名空间内明确专业化功能模板
- 为什么VS2017拒绝我的功能模板专业化,而不是VS2015拒绝
- 仅适用于单个功能的部分模板专业化
- 功能模板专业化具有模板打字名称
- 对完整模板专业类成员功能的未定义引用,但不是部分专业化
- 特定模板类型的功能模板专业化
- 功能模板专业化中的bool返回值
- C 将lambda函数用作模板功能专业化
- 如何正确设计和实施功能模板专业化
- Clang无法在模板类专业化中编译模板功能,该模板类专业化与模板声明具有 *不同的返回类型 *
- 统一功能使用模板专业化和接口来调用实例和原始类型
- 忽略了部分功能专业化中价值类型的最高级别const预选赛
- 功能专业化