如何将std::map传递到VS2013中的可变模板?
How can I pass a std::map to a variadic template in VS2013?
我有这个代码在VS2012十一月CTP工作:
//.h
template<template <typename...> class Container>
class Test {
typedef Container<unsigned int, TestEntry> L1;
Test();
...
}
//.cpp
template<template <typename...> class Container>
Test<Container>::Test() {}
...
template class Test<std::map>;
template class Test<std::unordered_map>;
//main.cpp
#include "test.h"
#include <map>
#include <unordered_map>
int main()
{
Test<std::map> test;
std::cout << "COMPILES!!!" << std::endl;
return 0;
}
我刚刚更新到Visual Studio 2013 Ultimate,它将无法编译,错误:
'std::map' : too few template arguments
我知道我可以放松模板的要求,如:
template< typename MapType>
但这对我来说不是一个很好的解决方案,因为我想自定义的唯一东西是容器类型,而不是内容。此外,内容很复杂,每次都要写,这是一个问题。
是否有办法解决这个在VS2013?我花了好几个小时想修好它,但还是没有成功。
更新:
在VS2013中精确复制:
//test.h
#include <map>
#include <unordered_map>
template<template <typename...> class Container>
class Test {
typedef Container<unsigned int, unsigned int> L1;
};
//test.cpp
#include "test.h"
template<> class Test<std::map> {
public:
typedef std::map<unsigned int, unsigned int> L1;
};
template<> class Test<std::unordered_map> {
public:
typedef std::unordered_map<unsigned int, unsigned int> L1;
};
//main.cpp
#include "test.h"
#include <iostream>
#include <cmath>
#include <map>
#include <unordered_map>
int main()
{
Test<std::map> test3;
std::cout << "COMPILES!!!" << std::endl;
return 0;
}
您在专门化中缺少<>
:
template<> class Test<std::map>
{
...
};
template<> class Test<std::unordered_map>
{
...
};
更新:
一个完整的专门化由三个令牌组成:
template
,<
和>
。声明时也需要相同的前缀全函数模板专门化。c++的早期设计语言没有包含这个前缀,而是添加了成员模板需要额外的语法来消除歧义专门化。[c++ Templates, Vandervoorde et al.].
一种方法是:
#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
template<template <typename...> class Container, typename ... T>
class Test
{
public:
typedef Container<T...> container_type;
static void print() { std::cout << "Generaln"; }
};
template<typename ... T>
class Test<std::map, T...>
{
public:
typedef std::map<T...> container_type;
static void print() { std::cout << "Mapn"; }
};
template<typename ... T>
class Test<std::unordered_map, T...>
{
public:
typedef std::unordered_map<T...> container_type;
static void print() { std::cout << "Unordered Mapn"; }
};
int main() {
Test<std::vector, int>::print();
Test<std::map, int, int>::print();
Test<std::unordered_map, int, int>::print();
}
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- 从 VS2013 到 VS2017 std::async 不会启动新线程
- std::与VS2013相比,GCC 4.7.2中的MAP实现效率非常低
- 为什么 boost::mutex 比 std::mutex 快 vs2013.
- GCC与VS2013中std::setprecision(0)的正确行为
- VS2013-输出窗口不显示std::cout
- std::make_pair C++11+VS2013=编译器错误
- 带有成员函数的VS2013 std::函数
- 如何将std::map传递到VS2013中的可变模板?