【发布时间】:2013-04-01 14:31:15
【问题描述】:
我不清楚返回 const 值对 C++11 中移动语义的影响。
这两个返回数据成员的函数有什么区别吗? const 在 C++11 中仍然是多余的吗?
int GetValueA() { return mValueA; }
const int GetValueB() { return mValueB; }
这些功能呢?
int GetValuesAB() { return mValueA + mValueB; }
const int GetValuesCD() { return mValueC + mValueD; }
【问题讨论】:
-
没有区别,因为
int是原始类型 -
@Cheersandhth.-Alf:准点! :)
-
@Alf 你是否暗示对于
std::string来说会有区别?如果有,是哪个? -
是的,在这种情况下我想说不是,但是返回一个非原始类型的 const 会做很多事情......在所有 const 限制之上,在 c++ 中11 它会有效地禁用rvalue move constructor。
标签: c++ c++11 constants move-semantics