How can I differentiate a pointer to a function lvalue and rvalue for overloaded functions?

I know I can do this to distinguish between the name of the rvalue function and the pointer to the lvalue function:

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 want to do the same for member functions. However, this does not seem to mean:

struct S;
void takeMemberFunction(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?

The alternative I know is to do for regular functions:

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);
}

What translates member functions:

struct S;
void takeMemberFunction(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);
}

But I would like to know that the discrepancy for my first example does not translate.

+4
source share
1 answer

, Visual ++, ( , ) gcc, clang, , :

struct S;

void bar(void (S::*& f)() ) {
    std::cout << "lvalue" << std::endl;
}
void bar(void (S::*&& p)() ) {
    std::cout << "rvalue" << std::endl;
}

struct S {
    void foo() { }  
};

int main() {
    void (S::*f)();

    bar(f);        // prints lvalue
    bar(&S::foo);  // prints rvalue
}

. ++?.

+4

All Articles