Hello,
I'm trying to improve the function binding for my script system. To do this, I need to retrive the return type of a certain class function at a certain point in the code. I'm working on MSVC2013 with C++11.
Without going into too much detail, here is some dummy code that shows what I am trying to accomplish:
template<typename Class>
class TypeInfo
{
public:
using ReturnType = std::result_of<decltype(&Class::Call)(...)>::type; // what goes to ... ?
};
// test classes
class Testing
{
public:
int Call(int) {}
};
class Testing2
{
public:
float Call(std::string, double) {}
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
TypeInfo<Testing>::ReturnType a = 1; // a should be "int"
TypeInfo<Testing2>::ReturnType b = 1.0f; b should be "float"
}
So what I want to do is pass an arbitrary class with a "Call" member function to the here-called TypeInfo-class, and this class should have the return-type of the call-function accessible as a typedef. I already found out that the std::result_of class can do this, BUT I cannot easily do it this way.
The problem is, that std::result_of needs to be called like this:
using ReturnType = std::result_of<decltype(&Testing::Call)(int)>::type;
using ReturnType = std::result_of<decltype(&Testing2::Call)(std::string, double)>::type;
where I explicitely need to specify the functions parameters after the decltype, which means that I cannot use this with functions with different function parameters, without specifiying them manually (what I do not want).
So the basic question is: Is there any way (ie. modifying my template class) so that result_of can be used with arbitrary function paramters?
If not, is there any other way using templates to extract the function return type in a manner that it is usable for me (I really do not care about things like multiple overloads of the same function etc...)?
I'm pretty sure there ought to be a sane solution for this, I'm just stuck here. I know I can make a templated function that achieves pretty much what I want:
template<typename Return, typename... Args>
void createReturnFunctionBinding(Return(*FunctionPointer)(Args...))
{
// now Return has the value of whichever function I pass in like...
}
int testing(float, double, int);
createReturnFunctionBinding(&testing); // Return is int
But I don't know how to make a class template that does the same, which I need here because I need the return type as a typedef. Any ideas?
Thanks in advance