0

I have a old varargs(...) function that uses va_list, va_start, va_arg, va_end

cell executeForwards(int id, ...)
{
    if (!g_forwards.isIdValid(id))
        return -1;

    cell params[FORWARD_MAX_PARAMS];
    
    int paramsNum = g_forwards.getParamsNum(id);
    
    va_list argptr;
    va_start(argptr, id);

    ForwardParam param_type;
    
    for (int i = 0; i < paramsNum && i < FORWARD_MAX_PARAMS; ++i)
    {
        param_type = g_forwards.getParamType(id, i);
        if (param_type == FP_FLOAT)
        {
            REAL tmp = (REAL)va_arg(argptr, double);            // floats get converted to doubles
            params[i] = amx_ftoc(tmp);
        }
        else if(param_type == FP_FLOAT_BYREF)
        {
            REAL *tmp = reinterpret_cast<REAL *>(va_arg(argptr, double*));
            params[i] = reinterpret_cast<cell>(tmp);
        }
        else if(param_type == FP_CELL_BYREF)
        {
            cell *tmp = reinterpret_cast<cell *>(va_arg(argptr, cell*));
            params[i] = reinterpret_cast<cell>(tmp);
        }
        else
            params[i] = (cell)va_arg(argptr, cell);
    }
    
    va_end(argptr);
    
    return g_forwards.executeForwards(id, params);
}

For some reason I need to push the parameters dynamically to this function, for example by using an array or something, Is that possible in C++?

example code:

va_list argptr;
va_push(argptr, 100);
va_push(argptr, 2.22f);
va_push(argptr, 300);
executeForwards(0, argptr);

Notice: I cannot modify the content of this function (executeForwards)

ChingSan
  • 11
  • 1
  • Your specific compiler might have something like that, but it's not part of standard C++. – Some programmer dude Mar 13 '22 at 12:55
  • Copy the function's source and modify the copy as needed? – Al.G. Mar 13 '22 at 13:05
  • Thanks for the reply, I'm using msvc, can you give me more details? – ChingSan Mar 13 '22 at 13:09
  • 1
    It is not possible to "build" a va_list, but you don't have to. Build a params array directly. Cut out the middle man. – Raymond Chen Mar 13 '22 at 13:17
  • Sorry, I cannot copy the function's source and modify, because it's a part of the core dll, and where I want to call the function was in a another dll, which means it's called remotely... – ChingSan Mar 13 '22 at 13:20
  • 1
    I do not understand. Just call `executeForwards(0, 100, 2.22f, 300)` – KamilCuk Mar 13 '22 at 13:27
  • Because I have a unknown parameters length at the compile time, so I have to push that dynamically. – ChingSan Mar 13 '22 at 13:32
  • There is no https://stackoverflow.com/questions/2210379/standard-way-to-manipulate-variadic-arguments. Try using libffi? – ecatmur Mar 13 '22 at 14:11
  • https://stackoverflow.com/questions/36548780/call-a-vararg-function-with-an-array – Al.G. Mar 13 '22 at 14:43
  • What is your actual assignment? What are the requirements and limitations? Who gave you this assignment, and who told you to use this function? Perhaps you should take a step back and talk to your colleagues, team members and team lead about this? – Some programmer dude Mar 13 '22 at 16:55

0 Answers0