Generate argument names from list of types to forward to another function

Viewed 63

I would like to generate a function that forwards its arguments to another function. I know that std::function does it somehow, but I cannot seem to find the right syntax myself.

It would be ok for my usecase to use som kind of template magic, but i want the user to be able to get information on the calling types when they use the function, like std::function does.

My usecase uses class member functions, so a solution that only works in that context is accepted. I tried to created the smallest example code possible.

#include <iostream>

// Macro usage cannot be changed 
#define ARGS int, int 

void otherFunction(int x, int y) {
    std::cout << x << "," << y << "\n";
}

// This is the behaviour i want
void expectedImplementation(int _1, int _2) {
    otherFunction(_1, _2);
}

// This works, but it prevents the user to view the expected
// types in their IDE
template <typename ...Args>
void templateVersion(Args ... args) {
    otherFunction(args...);
}

// This is the version I cannot get to work
// It does not need to look like this, but it needs to get
// its argument types from 
//void func(ARGS) {
//    otherFunction(/*how?*/);
//}

int main() {
    expectedImplementation(1, 2);
    templateVersion(1, 2);
    //func(1, 2);
}

godbolt

How do I accomplish this?

Edit: The function that needs to be forwarded to is also different for each instance of the function.

Edit 2: Ok, It seems like it's hard to specify the context without the context. Here is the actual code that I want to generalize. Here Is the template magick stuff where it should fit in. No memory should be used, otherwise I would just use the solution I have now with template parameter packs.

Edit 3: A better example:

#include <iostream>

#define ARGS int, int 

struct Inner {
    void otherFunction(int x, int y) {
        std::cout << x << y << std::endl;
    }
};

struct Wrapper {
    Inner inner;

    // This works, but it prevents the user to view the expected
    // types in their ide
    template <typename ...Args>
    void templateVersion(Args ... args) {
        inner.otherFunction(args...);
    }

    // The concept I try to figure out
    void function(ARGS) { // It does not need to look exactly like this
                          // , but functionally it needs to be somithing like it
        // Forward the arguments to the `ìnner` class
    }
};

int main() {
    auto wrapper = Wrapper{};
    wrapper.templateVersion(10, 20);
}
1 Answers

Your macro ARGS does not define named arguments. You cannot forward the type of unnamed arguments. That's a limitation of the language.

Either forget about using macros, and change your function definiton:

void func(int a, int b) {
    otherFunction(a, b);
}

Or change the definition of the macro:

#define ARGS   int a, int b

void func(ARGS) {
    otherFunction(a, b);
}

That said, nothing beats the template solution. Make sure you use perfect forwarding.

template <typename ...Args>
void templateVersion(Args&& ... args) {
   otherFunction(std::forward<Args>(args)...);
}
Related