2015-08-31 29 views
2

演繹類型時,我聲明如下常量:刪除CV預選賽使用declytype

const auto val = someFun(); 

現在我想用同一類型的「VAL」的,但沒有固定規範的另一個變量。

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

當前decltype保持常量。如何去除它?

+3

的''包含一個[ 'remove_cv'](http://en.cppreference.com/w/cpp/types/remove_cv)特徵,你可以使用它。 –

+0

謝謝,就是我在找的東西! – Arun

+0

除了Jarod42的回答和Bo的評論,請注意'auto nonConstVal = someOtherFun()'可能就足夠了,並且更具可讀性,這取決於您的其他代碼。當然,除非'someOtherFun()'返回* someFun()'的返回類型*。 – gd1

回答

3

<type_traits>

你可能在使用C++ 14:

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

或C++ 11

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

Demo