为什么在这种情况下调用非常量右值移动构造函数?



我已经看到了相关的问题,它们主要讨论我们是否应该将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 最初具有类型"cvT",其中Tcv非限定的非类、非数组类型,则表达式的类型将在任何进一步分析之前调整为T

翻译:该语言没有const限定的标量值。 它们根本不存在。

最新更新