Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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#exist...



My follow-up question is: why does decltype((value)) return int& in C++?



There 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.


Sorry, could not find a video.

See decltype section.


It 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'.


Because decltype(*&value) is int &.

The real question to ask is why decltype(value) is int.


That doesn't really answer the question. It's not obvious why decltype(*&value) is int& either.


Normally 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.


Thank you, I looked specifically whether this was discussed here already and found your reference.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: