Id-expression

0 views
Skip to first unread message

Amabella Batton

unread,
Aug 4, 2024, 1:24:48 PM8/4/24
to distrechiefu
Thephrase's intention is to disallow the usage of undeclared identifiers in expressions. I.e. this paragraph talks about id-expressions occurring as primary-expressions, not id-expressions occurring in declarators. I agree that the phrase is misleading and should be fixed. I filed a core issue.

Historical reasons; the subtle distinction between identifiers and names was made after the very fundamental grammatical constructs (id-expression, unqualified-id, etc.) were named, and from there onwards, renaming those wasn't an option.


So the question remains. Why are parentheses treated differently? Is anyone familiar with technical papers or committee discussions behind it? The explicit consideration for parentheses leads to think this is not an oversight, so there must be a technical reason I'm missing.


My Visual Studio 2019 suggest me to remove redundant parenthesis, but actually they turn into decltype((i)) which changes return value to int& which makes it UB since returning reference to a local variable.


The special provision is there so that we could write useful code more easily. When applying decltype to the name of a (member) variable, we don't usually want some type that represents the properties of the variable when treated as an expression. Instead we want just the type the variable is declared with, without having to apply a ton of type traits to get at it. And that's exactly what decltype is specified to give us.


Because of the nature of this information, the features had to be added in the language (it can't be done in a library). That means new keyword(s). The standard could have introduced two new keywords for this. For instance exprtype to get the type of an expression and decltype to get the declaration type of a variable. That would have been the clear, happy option.


However the standard committee has always tried its hardest to avoid introducing new keywords into the language in order to minimize breakage of old code. Backwards compatibility is a core philosophy of the language.


So with C++11 we got just one keyword used for two different things: decltype. The way it differentiates between the two uses is by treating decltype(id-expression) differently. It was a conscious decision by the committee, a (small) compromise.

3a8082e126
Reply all
Reply to author
Forward
0 new messages