C++ - 类方法函数指针unordered_map的初始化器列表
C++ - Initialiser list for unordered_map of class method function pointers
我是 C++11 的新手,所以我今天一直在试验std::function
,我正在尝试使用它为字符串创建一个哈希表,为我正在创建的小型汇编程序的类函数指针:
组装.cpp
#include <iostream>
#include <unordered_map>
#include <functional>
#include <vector>
#include <string>
class Assembler {
public:
// Assembles the source file and writes it
bool assemble(std::string output_file);
// Creates the symbol table from a (.s) file
bool create_symbol_table(std::string file_name);
private:
// Hash table for opcode to functions
std::unordered_map<std::string,std::function<bool(std::vector<std::string>)>>
opcode_map =
{
{"add",assemble_data_processing}
};
bool assemble_data_processing(std::vector<std::string> instruction);
};
bool Assembler::assemble_data_processing(std::vector<std::string> instruction) {
std::cout << "Data Processing" << std::endl;
return false;
}
这给了我一个 g++ 的编译时错误:
g++ -Wall -g -Werror -std=c++11 -o assemble.o assemble.cpp
assemble.cpp:28:5: error: could not convert ‘{{"add", ((Assembler*)this)->Assembler::assemble_data_processing}}’ from ‘<brace-enclosed initializer list>’ to ‘std::unordered_map<std::__cxx11::basic_string<char>, std::function<bool(std::vector<std::__cxx11::basic_string<char> >)> >’
};
^
但是,如果使该函数成为非类函数,那么它可以毫无问题地编译:
bool assemble_data_processing(std::vector<std::string> instruction) {
std::cout << "Data Processing" << std::endl;
return false;
}
如何在类方法的大括号初始化器列表中初始化unordered_map
?
您可以使用std::bind
std::unordered_map<std::string,std::function<bool(std::vector<std::string>)>>
opcode_map =
{
{"add", std::bind(&Assembler::assemble_data_processing, this, std::placeholders::_1}
};
或λ
std::unordered_map<std::string,std::function<bool(std::vector<std::string>)>>
opcode_map =
{
{"add", [this](std::vector<std::string> instruction){return assemble_data_processing(instruction);}}
};
或者将函数设为静态。
std::function
不能保存非绑定成员函数。
根据具体情况,您有三种方法可以解决此问题。
a( 如果assemble_data_processing
不使用任何成员数据,则将其设置为静态成员函数,并调用一天。
b( 如果您的缓存与单个Assembler
实例相关联,则使用std::bind
绑定函数,使用类似于以下内容的内容:
opcode_map =
{
{"add",std::bind(&Assembler::assemble_data_processing, this, _1)}
};
c( 如果Assembler
实例是稍后确定的,那么你将不得不放弃std::function
,并使用一个很好的旧函数指针指向成员函数。
- 使用通用值初始化 std::map,不重复
- 初始化 std::数组 of Eigen::Map
- std::map与谓词与初始化列表
- 当 map 是一个整数数组并且由 operator[] 创建时,它是否初始化其映射类型
- 如何修复"非聚合无法使用初始值设定项列表初始化" <map>
- C++仅在 std::map 中初始化密钥
- 将列表初始化的对插入到 std::map 中
- C++ 接受对抽象类的常量引用的构造函数无法初始化 std::map
- 如何在嵌套列表初始化中从map<><>中辨别向量?
- 初始化常量静态 std::map unique_ptr作为值
- 如何初始化 std::map<int, int> 键递增但值相同?
- 新的STD :: MAP条目如何仅通过键插入初始化
- 使用默认构造的值初始化 std::map
- STD :: MAP初始化列表构造函数
- 使用std::矢量进行std::map初始化
- C++中正确的Map初始化
- 使c++编译器优化到在map初始化时只调用构造函数一次
- c++ 0x 3d map初始化类似于php的关联数组
- Map初始化:目标代码比源代码大50倍
- 在map初始化列表中使用std::make_pair