7

コードをG++ ( gcc 4.8.1 およびMinGW 4.8.2 with -std=gnu++1yflag) でコンパイルすると、奇妙な動作が見つかりました。SSCCE の精神で、次のスニペットを分離します。

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

エラーが発生します:

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 &&) &&
     ^

ただし、#if 1and #if 0、 or #if 0andの場合は#if 1正常にコンパイルされます。また、すべてを に置き換えるautovoid、すべてが正常にコンパイルされます。

それはバグですか、それとも私の誤解ですか?

4

1 に答える 1