const int n = 0;
auto& n1 = const_cast<int&>(n);
auto n2 = const_cast<int&>(n);
Does the C++11 standard guarantee n2 is int&
by auto n2 = const_cast<int&>(n);
?
Must I use auto& n1 = const_cast<int&>(n);
instead of auto n2 = const_cast<int&>(n);
?
Are the two ways completely equivalent to each other as per the C++11 standard?
auto
uses the same rules as regular function template argument deduction, which never deduces a reference.
C++14 decltype(auto)
, on the other hand, can deduce a reference here. As well as C++11 auto&&
.
const int n = 0;
auto a = const_cast<int&>(n); // a is int
decltype(auto) b = const_cast<int&>(n); // b is int&
auto&& c = const_cast<int&>(n); // c is int&
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With