I am writing some macros that take a function name and do some actions, one of which is getting its signature through decltype and use it as a template parameter.
I want to extend these macros to support overloaded functions. My idea is to make them take an additional argument specifying the arguments list but not the complete signature, since two function overloads cannot be different for only the return type. The problem arises when I attempt to rebuild the complete signature of the function as I did in the non-overloaded case with decltype: how do I guess the return type?
I’ve tried with result_of, but with no luck:
#include <type_traits>
#include <typeinfo>
#include <iostream>
using namespace std;
double f(int, int);
int f(int, int, int);
int main(){
cout<<typeid(result_of<decltype(&f)(int,int, int)>::type).name()<<endl;
}
The snippet works if double f(int, int) is commented out. I understand that this is because within decltype it is unknown what of the two overloads to pick. Normally, one would prepend a dummy cast to a signature matching the desired overload, but that would mean that you know ahead the return type, hence all this stuff would be useless.
Is there a way to solve this problem, or do I have to enforce the user to specify the full signature in the macro?
EDIT:
Basically, I have these two macros:
//add a member function as a method
#define addmethod2(stringname, methodname) fields[#stringname]=method_helper<decltype(&hold_type::methodname), &hold_type::methodname>::worker
#define addmethod(methodname) addmethod2(methodname, methodname)
I want to add a third, addmethod3, which takes only the arguments list (if it was the complete signature with return type, it would be trivial), in order to select the correct member function overload.
What about
Then you can say