lundi 30 août 2021

Possible to use a variadic functions/templates in this way?

I was wondering whether given the below constraints, it is possible to use variadic functions/templates in order to pass a variable number of parameters (which are themselves a return value of a function) into a variadic function.

Constraints:

  1. No STL/Boost/other libraries.
  2. No loops/recursion.
  3. C++17 or earlier compliant (nothing beyond C++17).

Brief example:

using fn = F(...);
fn* func = (fn*) addr;
value = (*func)(pop_t(outputstack,o_stackidx,o_stacksize)...);
push_t(outputstack,o_stackidx,o_stacksize, value);

fn* is a function pointer (converted from a known user-defined memory address of a function) which takes in a variable number of pop_t arguments (values poped from a stack), these values (outputstack,o_stackidx,o_stacksize) are static and do not need to be variadic themselves. Essentially I was wondering whether it's possible to have the pop_t function repeat a variable number of times either A.) by depending on the appropriate number of arguments fn is capable of taking in or B.) using a user-defined integer specifying the number of repeats.

As an example say the user were to input a sin or atan2 function, these two functions take different numbers of parameters respectively being sin(x) and atan(y,x). For these two respective functions the function call expressions would look like the following:

sin -> (*func)(pop_t(outputstack,o_stackidx,o_stacksize)); 

atan2 -> (*func)(pop_t(outputstack,o_stackidx,o_stacksize),pop_t(outputstack,o_stackidx,o_stacksize)); 

Functions with N arguments pop N values from the stack by calling pop_t N times.

Reproducible example:

template<class U, class I>
U pop_t(U* stack, I &stackidx, I &stacksize) {
    if(stacksize>0) {
        stacksize--;
        stackidx = stackidx--;
        return stack[stackidx];
    }
    else {
        return stack[stackidx];
    }
}

int main() {
    float outputstack[2] = {3.141/2,1};
    o_stackidx = 2;
    o_stacksize = 2;
    long addr = (long)&atan2;
    using fn = F(...);
    fn* func = (fn*) addr;
    // Unknown variadic function pointer
    value = (*func)(pop_t(outputstack,o_stackidx,o_stacksize,nt)...);

    return 0;

}

Aucun commentaire:

Enregistrer un commentaire