首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法对也是函数的向量执行emplace_back操作

无法对也是函数的向量执行emplace_back操作
EN

Stack Overflow用户
提问于 2020-12-07 15:27:26
回答 1查看 99关注 0票数 0

所以,我尝试将lua字节码指令转换为向量,问题是这个函数的类型是向量,我试图向函数本身声明一个向量,也就是翻译器的向量。,emplace_back。例如:

代码语言:javascript
复制
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);行不起作用。

下面是我想要的名称:

代码语言:javascript
复制
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);
}

任何类似的事情对我来说都很好,我只是累了,什么都想不出来。

EN

回答 1

Stack Overflow用户

发布于 2020-12-07 15:53:25

不完全清楚你想要做什么,但我假设你想要的是:

代码语言:javascript
复制
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中,你需要声明一个vector来保存你的结果,然后返回它:

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65177582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档