3 ms·
My follow-up question is: why does decltype((value)) return int& in C++?
by loeg 4y ago
My follow-up question is: why does decltype((value)) return int& in C++?
- pjmlp 4y agoYou can find an explanation here, https://www.scs.stanford.edu/~dm/blog/decltype.html https://www.scs.stanford.edu/~dm/blog/decltype.html
- loeg 4y agoThere are a lot of words on that page and none really jump out at me as explaining why C++ chose to have decltype yield something different for decltype(value) and decltype((value)). But I was unable to read them all, so maybe it is buried somewhere in there.
- pjmlp 4y agoSorry, could not find a video. See decltype section.
- loeg 4y agoIt provides a series of rules for exactly when decltype() picks a reference or non-reference type, but as far as I can see, it does not explain why the language chose to sometimes return references in some scenarios, such as for '(value)' vs 'value'.
- pwdisswordfishc 4y agoBecause decltype(*&value) is int &. The real question to ask is why decltype(value) is int.
- loeg 4y agoThat doesn't really answer the question. It's not obvious why decltype(*&value) is int& either.
- edflsafoiewq 4y agoNormally decltype(E) returns the type of the expression E. The type of the expressions *&value and value are both int& for instance. However they also wanted a way to query the type an identifier was declared with, eg int x would be int. For some reason they chose to expose this by overriding decltype(E) when E is a plain identifier. decltype((value)) avoids this special case and returns the type of the expression (value), which is obviously the same as the expression value.