Search code examples
c++c++11overload-resolutionref-qualifierfunction-qualifier

call of overloaded with ref-qualifiers member function is ambiguous


I found a strange behaviour, when compliling my code with G++ (gcc 4.8.1 and MinGW 4.8.2 with -std=gnu++1y flag). In spirit of SSCCE I isolating the following snippet:

struct C
{

    template< typename X >
    auto
    f(X &&) const &
    { ; }

    template< typename X >
    auto
    f(X &&) &
    { ; }

    template< typename X >
    auto
    f(X &&) &&
    { ; }

};

int main()
{
    int i{};
#if 1
    C{}.f(i);
#endif
#if 1
    C c{};
    c.f(i);
#endif
    return 0;
}

It gives an error:

main.cpp: In function 'int main()':
main.cpp:29:10: error: call of overloaded 'f(int&)' is ambiguous
     c.f(i);
          ^
main.cpp:29:10: note: candidates are:
main.cpp:6:5: note: auto C::f(X&&) const & [with X = int&]
     f(X &&) const &
     ^
main.cpp:11:5: note: auto C::f(X&&) & [with X = int&]
     f(X &&) &
     ^
main.cpp:16:5: note: auto C::f(X&&) && [with X = int&]
     f(X &&) &&
     ^

But in case of #if 1 and #if 0, or #if 0 and #if 1 it compiles normally. Also if I replace all auto's with void's, then all compiles successfully too.

Is it bug, or just my misleading?


Solution

  • g++ 4.8.2 has the same problem with the even simpler (Live at coliru):

    struct A {
        auto f() & {}
        auto f() && {}
    };
    
    int main() {
        A{}.f();
        A a;
        a.f();
    }
    

    despite the program obviously being correct. It appears to be a bug in the interaction between ref-qualifiers and return type deduction: presumably the deduction process is stripping the qualifiers from the implicit object argument before handing them off to overload resolution.

    I have reported this as GCC bug 60943.