无法将 emplace_back 插入到同时也是函数的 vector 中

所以,我正在尝试将 lua 字节码指令 emplace_back 到一个 vector 中,问题在于这个函数的类型是 vector,我正在尝试将其 emplace_back 到函数自身,这样我就可以声明一个向翻译器 vector 中的 vector。 例如:

std::vector<Instruction*> translate_instr(lua_State* L, Proto* vP, int idx) {
    auto vanilla_instr = vP->code[inum];
    auto vanilla_op = GET_OPCODE(vanilla_instr);

    auto custom_instr_32 = U_CREATE_INSTR(); // 创建新的自定义指令

    switch (vanilla_op) {
        case OP_CALL:
        case OP_TAILCALL: {

            U_SET_OPCODE(luau_instr_32, custom_opcodes::MLUA_OP_CALL); // 将操作码的字节标识符设置为 OP_CALL 的自定义字节标识符

            U_SETARG_A(luau_instr_32, GETARG_A(vanilla_instr)); // 将第一个参数设置为自定义 a 参数
            U_SETARG_B(luau_instr_32, GETARG_B(vanilla_instr)); // 将第二个参数设置为自定义 B 参数
            U_SETARG_C(luau_instr_32, GETARG_B(vanilla_instr)); // 将第三个参数设置为自定义 C 参数

            translate_instr.push_back(custom_instr_32);

            break;
        }
    }
}

translate_instr.push_back(custom_instr_32); 这行不起作用。

这是我想要调用它的方式:

auto* L = luaL_newstate();
luaL_openlibs(L);
auto lcl = reinterpret_cast<const LClosure*>(lua_topointer(L, -1));
auto p = lcl->p;
for (auto i = 0; i < p->sizecode; ++i) {
std::vector<Instruction*> custom_instr_vec[i] = translate_instr(L, P, i);
}

与我类似的东西对我来说是好的,我只是累了,无法想出解决方法。

点赞
用户5494370
用户5494370

不完全清楚你想干嘛,但我假设你需要:

std::vector<Instruction*> custom_instr_vec;
for (auto i = 0; i < p->sizecode; ++i) {
  custom_instr_vec.push_back(translate_instr(L, P, i));
}

std::vector<Instruction*> custom_instr_vec[i] 将会声明一个大小为 i 的向量数组,但由于 i 不是编译时常数,所以它不是有效的 c++。

translate_instr 中,你需要声明一个向量来保存结果并将其返回:

std::vector<Instruction*> translate_instr(lua_State* L, Proto* vP, int idx) {
    std::vector<Instruction*> result;
    auto vanilla_instr = vP->code[inum];
    auto vanilla_op = GET_OPCODE(vanilla_instr);

    auto custom_instr_32 = U_CREATE_INSTR(); // 创建新的自定义指令

    switch (vanilla_op) {
        case OP_CALL:
        case OP_TAILCALL: {

            U_SET_OPCODE(luau_instr_32, custom_opcodes::MLUA_OP_CALL); // 将操作码的字节识别符设置为 OP_CALL 的自定义字节识别符

            U_SETARG_A(luau_instr_32, GETARG_A(vanilla_instr)); // 将第一个参数设置为自定义 a 参数
            U_SETARG_B(luau_instr_32, GETARG_B(vanilla_instr)); // 将第二个参数设置为自定义 b 参数
            U_SETARG_C(luau_instr_32, GETARG_B(vanilla_instr)); // 将第三个参数设置为自定义 c 参数

            result.push_back(custom_instr_32);

            break;
        }
    }
    return result;
}
2020-12-07 07:53:25