Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1179

Answer by HolyBlackCat for Why is low-level const of a reference not deduced?

$
0
0

Because it just... makes sense?

You don't want to strip non-top-level constness from a pointer/reference because that would be unsafe (potentially letting you modify a const object, which is UB).

But when creating a copy (using auto without &/&&), you don't really care about the constness of the original object. If you e.g. have a function returning const T & (or god forbid const T), it doesn't make you any more likely to want a const copy of the return value, compared to a function returning just T or T &.


And as noted by @user12002570's in his answer, auto ref2 = ref; //ref2 is int&! - the comment here is wrong, the type is deduced as just int.


Viewing all articles
Browse latest Browse all 1179

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>