C++ - 类方法函数指针unordered_map的初始化器列表

C++ - Initialiser list for unordered_map of class method function pointers

本文关键字:map 初始化 列表 unordered 类方法 函数 指针 C++      更新时间:2023-10-16

我是 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,并使用一个很好的旧函数指针指向成员函数。