A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://timsong-cpp.github.io/cppwp/n4659/stmt.stmt below:

[stmt.stmt]

9 Statements [stmt.stmt]

A name introduced by a declaration in a condition (either introduced by the decl-specifier-seq or the declarator of the condition) is in scope from its point of declaration until the end of the substatements controlled by the condition. If the name is redeclared in the outermost block of a substatement controlled by the condition, the declaration that redeclares the name is ill-formed. [Example:

if (int x = f()) {
  int x;            }
else {
  int x;            }

end example]

The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable contextually converted to bool. If that conversion is ill-formed, the program is ill-formed. The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise. The value of a condition that is an expression is the value of the expression, contextually converted to bool for statements other than switch; if that conversion is ill-formed, the program is ill-formed. The value of the condition will be referred to as simply “the condition” where the usage is unambiguous.

If a condition can be syntactically resolved as either an expression or the declaration of a block-scope name, it is interpreted as a declaration.

9.1 Labeled statement [stmt.label]

Case labels and default labels shall occur only in switch statements.

9.2 Expression statement [stmt.expr]

Expression statements have the form

expression-statement:
	expressionopt ;

The expression is a discarded-value expression. All side effects from an expression statement are completed before the next statement is executed. An expression statement with the expression missing is called a null statement. [Note: Most statements are expression statements — usually assignments or function calls. A null statement is useful to carry a label just before the } of a compound statement and to supply a null body to an iteration statement such as a while statement. end note]

9.4 Selection statements [stmt.select] 9.4.1 The if statement [stmt.if]

If the condition ([stmt.select]) yields true the first substatement is executed. If the else part of the selection statement is present and the condition yields false, the second substatement is executed. If the first substatement is reached via a label, the condition is not evaluated and the second substatement is not executed. In the second form of if statement (the one including else), if the first substatement is also an if statement then that inner if statement shall contain an else part.90

If the if statement is of the form if constexpr, the value of the condition shall be a contextually converted constant expression of type bool; this form is called a constexpr if statement. If the value of the converted condition is false, the first substatement is a discarded statement, otherwise the second substatement, if present, is a discarded statement. During the instantation of an enclosing templated entity, if the condition is not value-dependent after its instantiation, the discarded substatement (if any) is not instantiated. [Note: Odr-uses in a discarded statement do not require an entity to be defined. end note] A case or default label appearing within such an if statement shall be associated with a switch statement within the same if statement. A label declared in a substatement of a constexpr if statement shall only be referred to by a statement in the same substatement. [Example:

template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
  
  if constexpr (sizeof...(rs) > 0)
    g(rs...);       }

extern int x;       
int f() {
  if constexpr (true)
    return 0;
  else if (x)
    return x;
  else
    return -x;
}

end example]

9.4.2 The switch statement [stmt.switch]

The switch statement causes control to be transferred to one of several statements depending on the value of a condition.

The condition shall be of integral type, enumeration type, or class type. If of class type, the condition is contextually implicitly converted to an integral or enumeration type. If the (possibly converted) type is subject to integral promotions, the condition is converted to the promoted type. Any statement within the switch statement can be labeled with one or more case labels as follows:

case constant-expression :

where the constant-expression shall be a converted constant expression of the adjusted type of the switch condition. No two of the case constants in the same switch shall have the same value after conversion.

There shall be at most one label of the form

default :

within a switch statement.

Switch statements can be nested; a case or default label is associated with the smallest switch enclosing it.

When the switch statement is executed, its condition is evaluated and compared with each case constant. If one of the case constants is equal to the value of the condition, control is passed to the statement following the matched case label. If no case constant matches the condition, and if there is a default label, control passes to the statement labeled by the default label. If no case matches and if there is no default then none of the statements in the switch is executed.

case and default labels in themselves do not alter the flow of control, which continues unimpeded across such labels. To exit from a switch, see break. [Note: Usually, the substatement that is the subject of a switch is compound and case and default labels appear on the top-level statements contained within the (compound) substatement, but this is not required. Declarations can appear in the substatement of a switch statement. end note]

9.5 Iteration statements [stmt.iter]

The substatement in an iteration-statement implicitly defines a block scope which is entered and exited each time through the loop.

If the substatement in an iteration-statement is a single statement and not a compound-statement, it is as if it was rewritten to be a compound-statement containing the original statement. [Example:

while (--x >= 0)
  int i;

can be equivalently rewritten as

while (--x >= 0) {
  int i;
}

Thus after the while statement, i is no longer in scope. end example]

If a name introduced in an init-statement or for-range-declaration is redeclared in the outermost block of the substatement, the program is ill-formed. [Example:

void f() {
  for (int i = 0; i < 10; ++i)
    int i = 0;            for (int i : { 1, 2, 3 })
    int i = 1;          }

end example]

9.5.1 The while statement [stmt.while]

In the while statement the substatement is executed repeatedly until the value of the condition ([stmt.select]) becomes false. The test takes place before each execution of the substatement.

When the condition of a while statement is a declaration, the scope of the variable that is declared extends from its point of declaration to the end of the while statement. A while statement of the form

while (T t = x) statement

is equivalent to

label:
{                     T t = x;
  if (t) {
    statement
    goto label;
  }
}                   

The variable created in a condition is destroyed and created with each iteration of the loop. [Example:

struct A {
  int val;
  A(int i) : val(i) { }
  ~A() { }
  operator bool() { return val != 0; }
};
int i = 1;
while (A a = i) {
    i = 0;
}

In the while-loop, the constructor and destructor are each called twice, once for the condition that succeeds and once for the condition that fails. end example]

9.5.2 The do statement [stmt.do]

In the do statement the substatement is executed repeatedly until the value of the expression becomes false. The test takes place after each execution of the statement.

9.5.3 The for statement [stmt.for]

Either or both of the condition and the expression can be omitted. A missing condition makes the implied while clause equivalent to while(true).

If the init-statement is a declaration, the scope of the name(s) declared extends to the end of the for statement. [Example:

int i = 42;
int a[10];

for (int i = 0; i < 10; i++)
  a[i] = i;

int j = i;          

end example]

9.5.4 The range-based for statement [stmt.ranged]

The range-based for statement

for ( for-range-declaration : for-range-initializer ) statement

is equivalent to

{
	auto &&__range = for-range-initializer ;
	auto __begin = begin-expr ;
	auto __end = end-expr ;
	for ( ; __begin != __end; ++__begin ) {
		for-range-declaration = *__begin;
		statement
	}
}

where

[Example:

int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
  x *= 2;

end example]

9.6 Jump statements [stmt.jump]

On exit from a scope (however accomplished), objects with automatic storage duration that have been constructed in that scope are destroyed in the reverse order of their construction. [Note: For temporaries, see [class.temporary]. end note] Transfer out of a loop, out of a block, or back past an initialized variable with automatic storage duration involves the destruction of objects with automatic storage duration that are in scope at the point transferred from but not at the point transferred to. (See [stmt.dcl] for transfers into blocks). [Note: However, the program can be terminated (by calling std​::​exit() or std​::​abort() ([support.start.term]), for example) without destroying class objects with automatic storage duration. end note]

9.6.1 The break statement [stmt.break]

The break statement shall occur only in an iteration-statement or a switch statement and causes termination of the smallest enclosing iteration-statement or switch statement; control passes to the statement following the terminated statement, if any.

9.6.2 The continue statement [stmt.cont]

The continue statement shall occur only in an iteration-statement and causes control to pass to the loop-continuation portion of the smallest enclosing iteration-statement, that is, to the end of the loop. More precisely, in each of the statements

while (foo) {
  {
      }
contin: ;
}
do {
  {
      }
contin: ;
} while (foo);
for (;;) {
  {
      }
contin: ;
}

a continue not contained in an enclosed iteration statement is equivalent to goto contin.

9.6.3 The return statement [stmt.return]

A function returns to its caller by the return statement.

The expr-or-braced-init-list of a return statement is called its operand. A return statement with no operand shall be used only in a function whose return type is cv void, a constructor, or a destructor. A return statement with an operand of type void shall be used only in a function whose return type is cv void. A return statement with any other operand shall be used only in a function whose return type is not cv void; the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization from the operand. [Note: A return statement can involve an invocation of a constructor to perform a copy or move of the operand if it is not a prvalue or if its type differs from the return type of the function. A copy operation associated with a return statement may be elided or converted to a move operation if an automatic storage duration variable is returned ([class.copy]). end note] [Example:

std::pair<std::string,int> f(const char* p, int x) {
  return {p,x};
}

end example] Flowing off the end of a constructor, a destructor, or a function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function other than main results in undefined behavior.

The copy-initialization of the result of the call is sequenced before the destruction of temporaries at the end of the full-expression established by the operand of the return statement, which, in turn, is sequenced before the destruction of local variables ([stmt.jump]) of the block enclosing the return statement.

9.6.4 The goto statement [stmt.goto]

The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label located in the current function.

9.7 Declaration statement [stmt.dcl]

A declaration statement introduces one or more new identifiers into a block; it has the form

declaration-statement:
	block-declaration

If an identifier introduced by a declaration was previously declared in an outer block, the outer declaration is hidden for the remainder of the block, after which it resumes its force.

It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps91 from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer. [Example:

void f() {
    goto lx;            ly:
  X a = 1;
  lx:
  goto ly;                              }

end example]

Dynamic initialization of a block-scope variable with static storage duration or thread storage duration is performed the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.92 If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined. [Example:

int foo(int i) {
  static int s = foo(2*i);        return i+1;
}

end example]

The destructor for a block-scope object with static or thread storage duration will be executed if and only if it was constructed. [Note: [basic.start.term] describes the order in which block-scope objects with static and thread storage duration are destroyed. end note]

9.8 Ambiguity resolution [stmt.ambig]

[Note: If the statement cannot syntactically be a declaration, there is no ambiguity, so this rule does not apply. The whole statement might need to be examined to determine whether this is the case. This resolves the meaning of many examples. [Example: Assuming T is a simple-type-specifier,

T(a)->m = 7;        T(a)++;             T(a,5)<<c;          
T(*d)(int);         T(e)[5];            T(f) = { 1, 2 };    T(*g)(double(3));   

In the last example above, g, which is a pointer to T, is initialized to double(3). This is of course ill-formed for semantic reasons, but that does not affect the syntactic analysis. end example]

The remaining cases are declarations. [Example:

class T {
  public:
  T();
  T(int);
  T(int, int);
};
T(a);               T(*b)();            T(c)=7;             T(d),e,f=3;         extern int h;
T(g)(h,2);          

end example] end note]

The disambiguation is purely syntactic; that is, the meaning of the names occurring in such a statement, beyond whether they are type-names or not, is not generally used in or changed by the disambiguation. Class templates are instantiated as necessary to determine if a qualified name is a type-name. Disambiguation precedes parsing, and a statement disambiguated as a declaration may be an ill-formed declaration. If, during parsing, a name in a template parameter is bound differently than it would be bound during a trial parse, the program is ill-formed. No diagnostic is required. [Note: This can occur only when the name is declared earlier in the declaration. end note] [Example:

struct T1 {
  T1 operator()(int x) { return T1(x); }
  int operator=(int x) { return x; }
  T1(int) { }
};
struct T2 { T2(int){ } };
int a, (*(*b)(T2))(int), c, d;

void f() {
    T1(a) = 3,
  T2(4),                          (*(*b)(T2(c)))(int(d));                                       }

end example]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4