10

The following code compiles in Clang but not in GCC. When I call operator() with an lvalue it works, but not with an rvalue. Why?

struct S
{
    bool operator()() const &  { return true; }
    bool operator()() const && { return true; }
};

int main()
{
    S s;

    s();   // works
    S()(); // fails (error: call of '(S) ()' is ambiguous)
}

I'm compiling this code on GCC 4.8.

David G
  • 94,763
  • 41
  • 167
  • 253
  • Okay then, my first thought is GCC not including the ref qualifier in overload resolution. I suppose it's possible, what with 4.8 being the first version that supports them. – chris Jan 03 '14 at 01:16
  • clang happily compiles the code. gcc 4.9.0 20131222 (experimental) also doesn't like the code. – Dietmar Kühl Jan 03 '14 at 01:17
  • 3
    Try removing the `const` from the rvalue version, or otherwise say `static_cast(S())()`. – Kerrek SB Jan 03 '14 at 01:20
  • 3
    I just found this. It looks completely relevant: http://stackoverflow.com/questions/17130607/overload-resolution-with-ref-qualifiers. Didn't know that `const` made a difference, so hurray for learning every day. – chris Jan 03 '14 at 01:21
  • @KerrekSB Your first solution works, but not your second (on GCC at least). – David G Jan 03 '14 at 01:24
  • Is the & and && as a non method's parameter is a Clang extension or is defined in the C++11 standard ? – Guillaume Paris Jan 03 '14 at 07:47
  • @Guillaume07 They are defined in the Standard as reference qualifiers (I don't have access to the Standard right now so I can't show you the section in which they are defined). – David G Jan 03 '14 at 15:13

0 Answers0