gpt4 book ai didi

c++ - 使用模板作为参数时的函数模板特化

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:37 24 4
gpt4 key购买 nike

我想写一个函数模板来处理 vector 、列表、集合……并想编写专门化函数来单独处理 map ,当我写了下面的代码,编译器报错了。

谁能帮我修改一下?

#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特化中不存在。

关于c++ - 使用模板作为参数时的函数模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com