Стандарт C++ 11 (1119564), страница 45
Текст из файла (страница 45)
[ Note: For temporaries,see 12.2. — end note ] Transfer out of a loop, out of a block, or back past an initialized variable withautomatic storage duration involves the destruction of objects with automatic storage duration that are inscope at the point transferred from but not at the point transferred to. (See 6.7 for transfers into blocks).[ Note: However, the program can be terminated (by calling std::exit() or std::abort() (18.5), forexample) without destroying class objects with automatic storage duration. — end note ]6.6.11[stmt.break]The break statement shall occur only in an iteration-statement or a switch statement and causes terminationof the smallest enclosing iteration-statement or switch statement; control passes to the statement followingthe terminated statement, if any.6.6.21The break statementThe continue statement[stmt.cont]The continue statement shall occur only in an iteration-statement and causes control to pass to the loopcontinuation portion of the smallest enclosing iteration-statement, that is, to the end of the loop.
Moreprecisely, in each of the statementswhile (foo) {{// ...}contin: ;}do {{// ...}contin: ;} while (foo);for (;;) {{// ...}contin: ;}a continue not contained in an enclosed iteration statement is equivalent to goto contin.6.6.3The return statement[stmt.return]1A function returns to its caller by the return statement.2A return statement with neither an expression nor a braced-init-list can be used only in functions that donot return a value, that is, a function with the return type void, a constructor (12.1), or a destructor (12.4).§ 6.6.3136© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)A return statement with an expression of non-void type can be used only in functions returning a value; thevalue of the expression is returned to the caller of the function.
The value of the expression is implicitlyconverted to the return type of the function in which it appears. A return statement can involve theconstruction and copy or move of a temporary object (12.2). [ Note: A copy or move operation associatedwith a return statement may be elided or considered as an rvalue for the purpose of overload resolution inselecting a constructor (12.8). — end note ] A return statement with a braced-init-list initializes the objector reference to be returned from the function by copy-list-initialization (8.5.4) from the specified initializerlist.
[ Example:std::pair<std::string,int> f(const char* p, int x) {return {p,x};}— end example ]Flowing off the end of a function is equivalent to a return with no value; this results in undefined behaviorin a value-returning function.3A return statement with an expression of type void can be used only in functions with a return type of cvvoid; the expression is evaluated just before the function returns to its caller.6.6.41[stmt.goto]The goto statement unconditionally transfers control to the statement labeled by the identifier.
The identifiershall be a label (6.1) located in the current function.6.71The goto statementDeclaration statement[stmt.dcl]A declaration statement introduces one or more new identifiers into a block; it has the formdeclaration-statement:block-declarationIf an identifier introduced by a declaration was previously declared in an outer block, the outer declarationis hidden for the remainder of the block, after which it resumes its force.2Variables with automatic storage duration (3.7.3) are initialized each time their declaration-statement isexecuted. Variables with automatic storage duration declared in the block are destroyed on exit from theblock (6.6).3It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. Aprogram that jumps87 from a point where a variable with automatic storage duration is not in scope to apoint where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial defaultconstructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of thepreceding types and is declared without an initializer (8.5).
[ Example:void f() {// ...goto lx;// ...ly:X a = 1;// ...lx:goto ly;// ill-formed: jump into scope of a// OK, jump implies destructor// call for a followed by construction87) The transfer from the condition of a switch statement to a case label is considered a jump in this respect.§ 6.7© ISO/IEC 2011 – All rights reserved137ISO/IEC 14882:2011(E)// again immediately following label ly}— end example ]4The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storageduration (3.7.2) is performed before any other initialization takes place.
Constant initialization (3.6.2) of ablock-scope entity with static storage duration, if applicable, is performed before its block is first entered.An implementation is permitted to perform early initialization of other block-scope variables with static orthread storage duration under the same conditions that an implementation is permitted to statically initializea variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable isinitialized the first time control passes through its declaration; such a variable is considered initialized uponthe completion of its initialization.
If the initialization exits by throwing an exception, the initializationis not complete, so it will be tried again the next time control enters the declaration. If control entersthe declaration concurrently while the variable is being initialized, the concurrent execution shall wait forcompletion of the initialization.88 If control re-enters the declaration recursively while the variable is beinginitialized, the behavior is undefined. [ Example:int foo(int i) {static int s = foo(2*i);return i+1;}// recursive call - undefined— end example ]5The destructor for a block-scope object with static or thread storage duration will be executed if and onlyif it was constructed.
[ Note: 3.6.3 describes the order in which block-scope objects with static and threadstorage duration are destroyed. — end note ]6.81Ambiguity resolution[stmt.ambig]There is an ambiguity in the grammar involving expression-statements and declarations: An expressionstatement with a function-style explicit type conversion (5.2.3) as its leftmost subexpression can be indistinguishable from a declaration where the first declarator starts with a (. In those cases the statement is adeclaration. [ Note: To disambiguate, the whole statement might have to be examined to determine if it isan expression-statement or a declaration.
This disambiguates many examples. [ Example: assuming T is asimple-type-specifier (7.1.6),T(a)->m = 7;T(a)++;T(a,5)<<c;// expression-statement// expression-statement// expression-statementT(*d)(int);T(e)[5];T(f) = { 1, 2 };T(*g)(double(3));////////declarationdeclarationdeclarationdeclarationIn the last example above, g, which is a pointer to T, is initialized to double(3). This is of course ill-formedfor semantic reasons, but that does not affect the syntactic analysis. — end example ]2The remaining cases are declarations. [ Example:class T {// ...88) The implementation must not introduce any deadlock around execution of the initializer.§ 6.8138© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)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);////////declarationdeclarationdeclarationdeclaration// declaration— end example ] — end note ]3The 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.