模板专业和STD :: MAP
Template Specialization and std::map
我正在尝试使用相应的文本和呼叫功能创建Code
的地图。我会收到编译错误,我无法弄清楚我的代码,std::map
初始化是什么问题。有人可以解释一下我在做什么错,我该如何解决:
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <utility>
#include <map>
namespace mvs
{
enum Code
{
Code0 = -1,
Code1 = -2,
Code2 = 1,
CodeCount
};
}
class CompositeFile
{
public:
CompositeFile(std::string const& name) : name_(name) {}
template <typename T>
long readEx(mvs::Code code, std::vector<T>& buffer)
{
return 0;
}
std::string readString(mvs::Code code)
{
return {};
}
private:
std::string name_;
};
namespace mh
{
class CompositeFileEx : public CompositeFile
{
public:
CompositeFileEx(std::string const& name) : CompositeFile(name) {}
template <typename T>
std::string get(mvs::Code code)
{
std::vector<T> buffer;
readEx(code, buffer);
return {};
}
private:
typedef std::pair<std::string, std::function<std::string(mvs::Code)> > pair_type;
**std::map<mvs::Code, pair_type> map_ =
{
{ mvs::Code1, { "Code1", get<char>(mvs::Code1) } }
};**
};
template <>
std::string CompositeFileEx::get<char>(mvs::Code code)
{
return readString( code );
}
}
int main(int argc, char** argv)
{
return 0;
}
仔细查看如何定义对:
std::pair<std::string, std::function<std::string(mvs::Code)>>
第二个元素是std::function<std::string(mvs::Code)>
。构造时,您应该将可呼出的对象作为对的第二个参数。
表达式get<char>(mvs::Code1)
的类型不是将代码作为参数并返回字符串的可呼叫对象。表达式导致字符串直接。
要解决此问题,您应该发送一个将代码作为参数的lambda函数,然后返回get
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 使用一个考虑到std::map中键值的滚动或换行的键
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- C++如何创建 std::map
- 从其他容器中移动构造"std::map"
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 使用重载 [] 运算符返回 std::map() 的可赋值
- std::map, std::unordered_map - 缩小初始值设定项列表中的转换范围
- C++ 使用枚举类对象分配 std::map 值
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- std::map:当元素不可默认构造时创建/替换元素
- Arduino编译器和STL:使用std::vector和std::map