コードをG++ ( gcc 4.8.1 およびMinGW 4.8.2 with -std=gnu++1y
flag) でコンパイルすると、奇妙な動作が見つかりました。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 1
and #if 0
、 or #if 0
andの場合は#if 1
正常にコンパイルされます。また、すべてを に置き換えるauto
とvoid
、すべてが正常にコンパイルされます。
それはバグですか、それとも私の誤解ですか?