【发布时间】:2020-06-08 01:19:53
【问题描述】:
当有人问到decltype(a) 和decltype((a)) 的区别时,通常的答案是——a 是一个变量,(a) 是一个表达式。我觉得这个答案不令人满意。
首先,a 也是一个表达式。 primary expression 的选项包括 -
- (表达式)
- id 表达式
更重要的是,decltype considers parentheses very, very explicitly 的措辞:
For an expression e, the type denoted by decltype(e) is defined as follows:
(1.1) if e is an unparenthesized id-expression naming a structured binding, ...
(1.2) otherwise, if e is an unparenthesized id-expression naming a non-type template-parameter, ...
(1.3) otherwise, if e is an unparenthesized id-expression or an unparenthesized class member access, ...
(1.4) otherwise, ...
所以问题仍然存在。 为什么括号的处理方式不同?是否有人熟悉其背后的技术论文或委员会讨论?对括号的明确考虑导致认为这不是疏忽,所以一定有我遗漏的技术原因。
【问题讨论】:
-
“通常的答案是——a 是一个变量,(a) 是一个表达式” 他们的意思是“
(a)是一个表达式,而a是一个表达式和一个变量”。
标签: c++ c++11 language-lawyer decltype