【发布时间】:2015-08-31 08:51:57
【问题描述】:
我有一个常量声明如下:
const auto val = someFun();
现在我想要另一个具有相同类型“val”但没有常量规范的变量。
decltype(val) nonConstVal = someOtherFun();
// Modify nonConstVal, this is error when using decltype
目前 decltype 保持常量。如何剥离?
【问题讨论】:
-
<type_traits>包含一个remove_cv特征,您可以使用它。 -
谢谢,正是我想要的!
-
除了 Jarod42 的回答和 Bo 的评论之外,请注意
auto nonConstVal = someOtherFun()可能就足够了,并且更具可读性,具体取决于您的代码的其余部分。当然,除非someOtherFun()返回的东西不完全是someFun()的返回类型。
标签: c++ decltype type-deduction