samedi 18 avril 2015

How do I differentiate an lvalue and rvalue member function pointer for overloaded functions?

I know that I can do this to differentiate a rvalue function name and an lvalue function pointer:



template <typename RET_TYPE, typename...ARGs>
void takeFunction(RET_TYPE(& function)(ARGs...))
{
cout << "RValue function" << endl;
}

template <typename RET_TYPE, typename...ARGs>
void takeFunction(RET_TYPE(*& function)(ARGs...))
{
cout << "LValue function" << endl;
}

void function()
{
}

void testFn()
{
void(*f)() = function;
takeFunction(function);
takeFunction(f);
}


And I wish to do the same for member functions. However, this doesn't work



struct S;
void takeeMemberFunction(void(S::&function)()) // error C2589: '&' : illegal token on right side of '::'
{
cout << "RValue member function" << endl;
}

void takeMemberFunction(void(S::*&function)())
{
cout << "LValue member function" << endl;
}

struct S
{
void memberFunction()
{
}
};

void testMemberFn()
{
void(S::*mf)() = &S::memberFunction;
takeMemberFunction(S::memberFunction);
takeMemberFunction(mf);
}


Why?


Also, this doesn't work:



struct S;
void takeeMemberFunction(void(S::*&&function)())
{
cout << "RValue member function" << endl;
}

void takeMemberFunction(void(S::*&function)())
{
cout << "LValue member function" << endl;
}

struct S
{
void memberFunction()
{
}
};

void testMemberFn()
{
void(S::*mf)() = &S::memberFunction;
takeMemberFunction(&S::memberFunction); // error C2664: 'void takeMemberFunction(void (__thiscall S::* &)(void))' : cannot convert argument 1 from 'void (__thiscall S::* )(void)' to 'void (__thiscall S::* &)(void)'
takeMemberFunction(mf);
}


Whereas this does:



void takeFunction(void(*&& function)())
{
cout << "RValue function" << endl;
}

void takeFunction(void(*& function)())
{
cout << "LValue function" << endl;
}

void function()
{
}

void testFn()
{
void(*f)() = function;
takeFunction(&function);
takeFunction(f);
}


Again why?


Aucun commentaire:

Enregistrer un commentaire