When is a C++ expression well formed?

∥☆過路亽.° 提交于 2019-12-06 00:42:17

问题


Skimming through the C++ standard I came in quite a few cases to the statement:

The expression X shall be well formed.

I said to my self "OK, intuitively you know what a well formed expression is, but can you give a formal explanation of what makes a C++ expression a well formed expression?".

I searched a little bit and I didn't find anything that gives a formal explanation on the matter. So here's my question:

Q: What are the qualitative characteristics of a well formed expression in C++?


回答1:


A well-formed expression must conform to the grammar for an expression (as defined by the standard) and must conform to the semantic rules, such as not using names which have not been declared, or not redeclaring a name in the same scope with a different meaning.

i = 0

X::i++

The expressions above are syntactically valid, but if i has not been declared, or is const, or X is not a namespace or class type, or X::i has not been declared, or X::i does not support post-increment, then they fail to meet the semantic requirements for a well-formed expression.

Q: What are the qualitative characteristics of a well formed expression in C++?

See Clauses 1 to 15. You can't reduce the entire C++ language to a simple list.




回答2:


C++ Standard does not define well-formed expression, though it actually uses this phrase. There is definition of well-formed program

1.3.26 [defns.well.formed] well-formed program C++ program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition Rule (3.2).

I guess we can assume that well-formed expression is an expression which does not make the program ill-formed (which is defined in 1.3.9 as not well formed).



来源:https://stackoverflow.com/questions/25335467/when-is-a-c-expression-well-formed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!