While answering this question I wrote this working code, wrapping function passed in template arguments:
template<typename Fn, Fn fn, typename... Args>
auto wrapper(Args... args)->decltype(fn(args...)){
return fn(args...);
}
#define WRAPPER(FUNC) wrapper<decltype(&FUNC), &FUNC>
Example usage (I use this code for testing):
int min(int a, int b){
return (a<b)?a:b;
}
#include<iostream>
using std::cout;
int main(){
cout<<WRAPPER(min)(10, 20)<<'\n';
}
Two people told me to use perfect forwarding. When I asked how to do this, one of them redirected me here. I read question, carefully read best answer, and changed wrapper
to:
#include<utility>
template<typename Fn, Fn fn, typename... Args>
auto wrapper(Args&&... args)->decltype(fn(std::forward<Args...>(args...))){
return fn(std::forward<Args...>(args...));
}
It compiles, unless I attempt to check it out using example code above. How can I fix the code?