I am using a C++preprocessor to batch generate function definitions, but there is an issue when passing parameters
#include <iostream>
#define FOR_EACH_FUNC(_) \
_(int, add, int a, int b) \
_(int, subtract, int a, int b)
#define FORWARD_FUNC(ret, func, ...) \
ret func(__VA_ARGS__) { \
impl->func(__VA_ARGS__); \ // error!
} \
FOR_EACH_FUNC(FORWARD_FUNC)
int main()
{
int r1 = add(1, 2);
int r2 = subtract(1, 2);
return 0;
}
The result I want is
int add(int a, int b) {
impl->add(a, b);
}
But the current result is
int add(int a, int b) {
impl->add(int a, int b);
}
Is there any way to solve this problem? Thanks
I tried using C++'s variable parameter template to solve it, but it was not successful
Supplementary explanation:
We have a library A that needs to be used by a third party, but it relies on many other libraries. In order to clean up the compilation dependencies of others, we have implemented an intermediate library B that calls the methods of A through dlopen/dlsys (library A and its dependencies will be built into our system).
The problem here is: to implement a function, we need to first implement it in A, then define it in B as well, then define the function pointer, find the corresponding function address, and implement a simple logic to forward the request to A. I want to simplify this process
If all your arguments are trivially copyable (which stuff crossing a DLL boundary should be), you can use this:
Which will just be a bracketed name in the function declaration but be a functional cast as an expression.
This won't work with multi-word type names like
unsigned long long, but you can use a typedef (using ull = unsigned long long;) orstd::type_identity_t<unsigned long long>.If you don't mind automatically named parameters (
int _0, int _1), you can do this with Boost.Preprocessor: