为什么 B 的临时对象调用 B::f 时选择 void B::f() const & ?
Why is void B::f() const & chosen when B::f is called by a temporary object of B?
#include <iostream>
struct A
{
void f() const &
{
std::cout << "A::f()&" << std::endl;
}
void f() const &&
{
std::cout << "A::f()&&" << std::endl;
}
};
struct B
{
void f() const &
{
std::cout << "B::f()&" << std::endl;
}
};
int main()
{
A{}.f();
B{}.f();
}
输出为:
A::f()&&
B::f()&
请注意 void B::f() const &&
不存在。
对我来说,应该选择 B
调用 B::f
、void B::f() const &&
的临时对象,否则会引发编译错误。
为什么在这种情况下选择void B::f() const &
?
因为 void B::f() const &&
不存在,所以选择下一个最佳候选人,即您的 void B::f() const &
。右值将绑定到 const &
。如果你删除 const 你会注意到你会得到一个编译错误,因为右值不能绑定到非常量引用。 cppreference/overloadresolution 上的示例完美地展示了它。
int i;
int f1();
int g(const int&); // overload #1
int g(const int&&); // overload #2
int j = g(i); // lvalue int -> const int& is the only valid conversion
int k = g(f1()); // rvalue int -> const int&& better than rvalue int -> const int&
这与您示例中的隐式 this
参数没有什么不同。
To me, a temporary object of B calls B::f, void B::f() const && should be chosen, or a compiler error should be raised.
如果是这种情况,那么 following 之类的代码将在没有 [const] 右值引用重载的情况下中断。
void f(const int& i)
{
std::cout << i;
}
int main()
{
f(3);
}
#include <iostream>
struct A
{
void f() const &
{
std::cout << "A::f()&" << std::endl;
}
void f() const &&
{
std::cout << "A::f()&&" << std::endl;
}
};
struct B
{
void f() const &
{
std::cout << "B::f()&" << std::endl;
}
};
int main()
{
A{}.f();
B{}.f();
}
输出为:
A::f()&&
B::f()&
请注意 void B::f() const &&
不存在。
对我来说,应该选择 B
调用 B::f
、void B::f() const &&
的临时对象,否则会引发编译错误。
为什么在这种情况下选择void B::f() const &
?
因为 void B::f() const &&
不存在,所以选择下一个最佳候选人,即您的 void B::f() const &
。右值将绑定到 const &
。如果你删除 const 你会注意到你会得到一个编译错误,因为右值不能绑定到非常量引用。 cppreference/overloadresolution 上的示例完美地展示了它。
int i;
int f1();
int g(const int&); // overload #1
int g(const int&&); // overload #2
int j = g(i); // lvalue int -> const int& is the only valid conversion
int k = g(f1()); // rvalue int -> const int&& better than rvalue int -> const int&
这与您示例中的隐式 this
参数没有什么不同。
To me, a temporary object of B calls B::f, void B::f() const && should be chosen, or a compiler error should be raised.
如果是这种情况,那么 following 之类的代码将在没有 [const] 右值引用重载的情况下中断。
void f(const int& i)
{
std::cout << i;
}
int main()
{
f(3);
}