【问题标题】:Cannot emplace_back to a vector that is also a function不能 emplace_back 到同时也是函数的向量
【发布时间】:2021-03-18 12:21:16
【问题描述】:

所以,我正在尝试将 lua 字节码指令 emplace_back 放入向量中,问题是它所在的函数具有向量类型,我正在尝试将 emplace_back 放入函数本身,这样我可以声明一个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(); // Creates new custom instruction

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

            U_SET_OPCODE(luau_instr_32, custom_opcodes::MLUA_OP_CALL); // Set opcode's byte identifier to custom byte identifier of OP_CALL

            U_SETARG_A(luau_instr_32, GETARG_A(vanilla_instr)); // Set first arg to custom a arg
            U_SETARG_B(luau_instr_32, GETARG_B(vanilla_instr)); // Set 2nd arg to custom B arg
            U_SETARG_C(luau_instr_32, GETARG_B(vanilla_instr)); // Set third arg to custom C arg

            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);
}

任何类似的事情都对我有好处,我只是累了,想不通。

【问题讨论】:

  • 请不要添加无关的语言标签。这与 C 无关。
  • 当你说“不起作用”时,你是什么意思?请edit您的问题澄清。就像如果您遇到构建错误一样,请将完整且完整的错误输出复制粘贴(作为文本)到问题中。
  • 帕斯卡背景?在 C++ 中,您必须使用 return 语句,而不是使用函数名作为结果。看来您需要一本好的 C++ 教科书并学习语言基础知识。

标签: c++ bytecode luac


【解决方案1】:

尚不清楚您要做什么,但我认为您想要的是:

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&lt;Instruction*&gt; custom_instr_vec[i] 将声明一个大小为 i 的向量数组,因为 i 不是编译时间常数,因此是无效的 c++。

translate_instr 中你需要声明一个vector 来保存你的结果然后返回它:

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(); // Creates new custom instruction

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

            U_SET_OPCODE(luau_instr_32, custom_opcodes::MLUA_OP_CALL); // Set opcode's byte identifier to custom byte identifier of OP_CALL

            U_SETARG_A(luau_instr_32, GETARG_A(vanilla_instr)); // Set first arg to custom a arg
            U_SETARG_B(luau_instr_32, GETARG_B(vanilla_instr)); // Set 2nd arg to custom B arg
            U_SETARG_C(luau_instr_32, GETARG_B(vanilla_instr)); // Set third arg to custom C arg

            result.push_back(custom_instr_32);

            break;
        }    
    }
    return result;
}

【讨论】:

  • @Slava 评论不是提问的好地方
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 2021-10-12
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多