简体   繁体   English

使用Declytype推导类型时删除CV限定词

[英]Removing CV qualifiers when deducing types using declytype

I have a constant declared as following: 我有一个常量声明如下:

const auto val = someFun();

Now I want another variable with same type of 'val' but without the constant specification. 现在,我想要另一个具有相同类型“ val”但没有常量说明的变量。

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

Currently decltype keeps the constness. 当前,decltype保持常数。 How to strip it? 如何剥离?

From <type_traits> 来自<type_traits>

You may use in C++14: 您可以在C ++ 14中使用:

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

or in C++11 或在C ++ 11中

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

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM