【发布时间】:2016-12-06 10:29:56
【问题描述】:
const int n = 0;
auto& n1 = const_cast<int&>(n);
auto n2 = const_cast<int&>(n);
C++11 标准是否保证 n2 is int& by auto n2 = const_cast<int&>(n);?
我必须使用auto& n1 = const_cast<int&>(n); 而不是auto n2 = const_cast<int&>(n);吗?
根据 C++11 标准,这两种方式是否完全等价?
【问题讨论】:
-
很确定
n2是int,没有任何参考。auto基本遵循模板实参推演规则。 -
请注意,使用 C++14 的
decltype(auto),您可以应用decltype规则,您将获得int&。
标签: c++ c++11 auto type-systems type-deduction