4

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, it doesn't seem to translate:

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?

An alternative I know is to do this 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);
}

Which does translate to 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 the discrepancy for my first example not translating.

Adrian
  • 10,246
  • 4
  • 44
  • 110
  • _"this doesn't work"_ :( – Lightness Races in Orbit Apr 18 '15 at 16:43
  • @LightningRacisinObrit: Huh? Not a useful comment. – Adrian Apr 18 '15 at 16:45
  • Actually it is if you read it as "'this doesn't work' is not a useful problem description". The hope was to push you into improving it without going into explaining the obvious! – Lightness Races in Orbit Apr 18 '15 at 16:52
  • Since when are function names rvalues? – T.C. Apr 18 '15 at 17:16
  • @LightningRacisinObrit, not really. I was just cleaning it up, regardless of your comment. – Adrian Apr 18 '15 at 17:16
  • @T.C. well, I don't know what else they would be. – Adrian Apr 18 '15 at 17:22
  • @Adrian: Next time please post the question its completed form the first time around. – Lightness Races in Orbit Apr 18 '15 at 17:25
  • @LightningRacisinObrit, like you don't make mistakes. Check out what you wrote. ^^ :D We write and we rewrite to try and make it clearer, there's nothing wrong with that. – Adrian Apr 18 '15 at 17:35
  • They are lvalues and always have been. There are no rvalues of function type. – T.C. Apr 18 '15 at 17:37
  • @T.C., you can assign to an lvalue, you cannot assign to a function name. Thus it is an rvalue (or a prvalue if you go outside of the definitions of just lvalue and rvalue). QED – Adrian Apr 18 '15 at 18:07
  • By your logic, given `const int i = 5;` `i` would be an rvalue. Have you actually read the standard? – T.C. Apr 18 '15 at 18:18
  • @T.C. That's an interesting case. `i` was on the left hand side at one point, so it *might* be considered to be an lvalue at that point. But after that, I think it *would* be considered an rvalue. I've read parts of the standard, though usually not directly. Say, like in [this case](http://stackoverflow.com/a/3601748/1366368) where it has been quoted. I take it you've read all of the language laws and understand them 100%? – Adrian Apr 18 '15 at 18:29
  • 1
    If you actually read that [basic.lval] quote, you'd see that "An lvalue [...] designates a function or an object." Also, [conv.fptr]/p1: "An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function." [expr.prim.general]/p8: "An *identifier* is an *id-expression* provided it has been suitably declared (Clause 7). [...] The type of the expression is the type of the identifier. The result is the entity denoted by the identifier. **The result is an lvalue if the entity is a function**, variable, or data member and a prvalue otherwise." – T.C. Apr 18 '15 at 18:32
  • @T.C., I'll have to mull that over and look over the standard a little. However, how do you think I should have worded the question then? – Adrian Apr 18 '15 at 18:44

1 Answers1

4

I'm guessing this is a Visual C++ bug, as the following code (basically what you have in your question) compiles for me on both gcc and clang, and I see no reason to expect it not to:

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
}

For the other part of your question, see Why doesn't reference-to-member exist in C++?.

Community
  • 1
  • 1
Barry
  • 286,269
  • 29
  • 621
  • 977