在使用declytype推断类型时删除CV限定符

Removing CV qualifiers when deducing types using declytype

本文关键字:CV 删除 类型 declytype      更新时间:2023-10-16

我有一个常量声明如下:

const auto val = someFun();

现在我想要另一个变量具有相同类型的'val',但没有常量规格。

decltype(val) nonConstVal = someOtherFun();
// Modify nonConstVal, this is error when using decltype

当前decltype保留常量。怎么剥?

From <type_traits>

可以在c++ 14中使用:

std::remove_cv_t<decltype(val)> nonConstVal = someOtherFun();

或c++ 11

std::remove_cv<decltype(val)>::type nonConstVal = someOtherFun();
演示