5 ms·
> C++11 standardized a similar feature under the name decltype, so sadly we wound up with two names for a nearly identical feature. If you wonder why: https:/
by jwilk 3y ago
> C++11 standardized a similar feature under the name decltype, so sadly we wound up with two names for a nearly identical feature.
If you wonder why:
https://open-std.org/JTC1/SC22/WG14/www/docs/n2927.htm#existing-decltype https://open-std.org/JTC1/SC22/WG14/www/docs/n2927.htm#exist...
- meinersbur 3y agoThank you, I looked specifically whether this was discussed here already and found your reference.
- loeg 3y agoMy follow-up question is: why does decltype((value)) return int& in C++?
- pjmlp 3y 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 3y 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 3y agoSorry, could not find a video. See decltype section.
- loeg 3y 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 3y agoBecause decltype(*&value) is int &. The real question to ask is why decltype(value) is int.
- loeg 3y agoThat doesn't really answer the question. It's not obvious why decltype(*&value) is int& either.
- edflsafoiewq 3y 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.