gpt4 book ai didi

c++ - 使用 declytype 推导类型时删除 CV 限定符

转载 作者:搜寻专家 更新时间:2023-10-31 01:38:46 28 4
gpt4 key购买 nike

我有一个常量声明如下:

const auto val = someFun();

现在我想要另一个具有相同类型“val”但没有常量规范的变量。

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

目前 decltype 保持常量。如何剥离?

最佳答案

来自 <type_traits>

你可以在 C++14 中使用:

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

或者在 C++11 中

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

Demo

关于c++ - 使用 declytype 推导类型时删除 CV 限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32307342/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com