【发布时间】:2018-12-06 14:45:36
【问题描述】:
我看过相关的问题,他们主要讨论是否应该将 const 右值引用作为参数。但我仍然无法解释为什么在以下代码中调用了 非 const 移动构造函数:
#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());
}
【问题讨论】:
-
与您的问题无关,但是从函数返回
const值确实毫无意义。无论如何都不会阻止调用者将值存储在非常量变量中。 -
@Someprogrammerdude,那你怎么能得到 const rvalues 呢?
-
只是为了好玩,try your sample with a non-scalar type。 ttbomk 标量的 const prvalue 是虚构的。
-
@sanjivgupta 常量“值”是什么类型的?你的意思是?用例是什么?
-
@sanjivgupta 没有
const int右值这样的东西。
标签: c++ language-lawyer move-semantics