Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a C++ expression well formed?

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++?

like image 401
101010 Avatar asked Aug 15 '14 23:08

101010


2 Answers

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

like image 152
Wojtek Surowka Avatar answered Oct 22 '22 14:10

Wojtek Surowka


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.

like image 25
Jonathan Wakely Avatar answered Oct 22 '22 15:10

Jonathan Wakely