我已经看到了相关的问题,它们主要讨论我们是否应该将const rvalue引用作为参数。但我仍然无法推理为什么在以下代码中调用非常量移动构造函数:
#include <iostream>
using namespace std;
class A
{
public:
A (int const &&i) { cout << "const rvalue constructor"; }
A (int &&i) { cout << "non const rvalue constructor"; }
};
int const foo (void)
{
const int i = 3;
return i;
}
int main (void)
{
A a(foo());
}
下面是代码的略微修改版本:
#include <iostream>
#if 0
using T = int;
#else
struct T {T(int){}};
#endif
using namespace std;
class A {
public:
A (T const &&i) { cout << "const rvalue constructor"; }
A (T &&i) { cout << "non const rvalue constructor"; }
};
T const
foo (void)
{
const T i = 3;
return i;
}
int main()
{
A a(foo());
}
当T == int
时,你会得到非常量重载。 当T
是类类型时,你会得到 const 重载。 此行为不属于第 8.2.2 节 [expr.type]/p2:
如果 prvalue 最初具有类型"cv
T
",其中T
是cv非限定的非类、非数组类型,则表达式的类型将在任何进一步分析之前调整为T
。
翻译:该语言没有const
限定的标量值。 它们根本不存在。