对重载了ref限定符的成员函数的调用不明确
call of overloaded with ref-qualifiers member function is ambiguous
当我用G++编译代码时(gcc4.8.1和MinGW4.8.2带有-std=gnu++1y
标志),我发现了一个奇怪的行为。本着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
和#if 0
,或#if 0
和#if 1
的情况下,它可以正常编译。此外,如果我用void
替换所有的auto
,那么所有的编译也会成功。
是bug,还是我的误导?
g++4.8.2与更简单的(住在coliru)有同样的问题:
struct A {
auto f() & {}
auto f() && {}
};
int main() {
A{}.f();
A a;
a.f();
}
尽管程序显然是正确的。这似乎是ref限定符和返回类型推导之间的交互中的一个错误:推断过程可能是在将限定符交给重载解析之前,从隐式对象参数中剥离这些限定符。
我已经将此报告为GCC错误60943。
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法