为什么 static_cast 等运算符有一定的优先级?
根据 cppreference.com,C++ static_cast
运算符的优先级为2.。
为什么要定义这些级别?我想不出任何理由。谁能提供一个例子吗?
According to cppreference.com, the C++ static_cast
operator's level of precedence is 2.
Why are those levels even defined? I can't think of any reason. Can anyone provide an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该标准没有定义优先级;这些可以从语法派生。
与任何其他语法功能一样,
static_cast
在此语法中占有一席之地。因为它的使用需要括号,所以它的操作数表达式永远不会有歧义,但这仅仅意味着从语法中为其派生优先级是没有意义的,并不是说它在语法本身中的位置是没有意义的。因此,该标准在这里并没有做任何疯狂的事情。毫无意义的是,无论您引用的来源是什么,都列出了
static_cast
的优先级。这不是错误,只是毫无意义。The standard doesn't define precedence levels; these can be derived from the grammar.
Like any other syntactical feature,
static_cast
has a place in this grammar. Because its use requires parentheses its operand expression can never be ambiguous, but that only means that it makes no sense to bother deriving a precedence level for it from the grammar, not that its place in the grammar itself is meaningless. Thus the standard is doing nothing crazy here.What's pointless is that whatever source you cited listed a precedence level for
static_cast
. It's not wrong, it's just pointless.这是谁说的?该标准没有定义运算符优先级。它以类似 BNF 的表示法定义语法。
Who said this? The standard doesn't define operator precedence. It defines the grammar in a BNF-like notation.