简体   繁体   中英

Cannot emplace_back to a vector that is also a function

So, I'm attempting to emplace_back a lua bytecode instruction into a vector, the issue is the function this is in has the type of vector, I'm trying to emplace_back to the function itself, that way I could declare a vector the the vector of the translator. Ex:

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

The line translate_instr.push_back(custom_instr_32); doesn't work.

Here's how I want to call it:

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

Anything similar is good with me, I just am tired and can't figure anything out.

Its not entirely clear what you are trying to do but I assume what you want is:

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] would declare an array of vectors of size i which as i is not a compile time constant is not valid c++.

In translate_instr you need to declare a vector to hold your result and then return it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM