It is required to set a variable bounded by the scope of a conditional if or switch . How can I do that?

2 answers 2

In C ++, it was originally possible to declare variables in conditions. In C ++ 11, these capabilities were expanded by introducing the concept of contextual transformation and the ability to use an initialization list in curly braces. According to C ++ standard (6.4 Selection statement)

2 for the rules for conditions and for the while statements (6.5). The declarator should not specify a function or an array. The decl-speci-seq shall not define a class or enumeration. If you鈥檙e auto-type, you can鈥檛 say that you鈥檝e been as it is described in 7.1.6.4.

3 A name of a condition or a statement on the condition of the condition. The name is ill-formed.

This is a statement of the context variable converted to a bool (Clause 4). If that conversion is ill-formed, the programisill-formed. It is a rule that you can change the number of words. The context of the expression is the context of the expression; if that conversion is ill-formed, the program is ill-formed. The condition of the condition is where the usage is unambiguous.

That is, instead of expressions in terms of sentences such as if , switch , for , while you can include an oblique oblique, which has an initializer. In the if , for , and while clauses, the value of the declared variable is contextually converted to the bool type, and this value is used as the value of the condition.

You cannot use declarations of arrays or functions as declarations in these sentences, or define classes or enums.

For a switch clause, the declared variable must have an integer type or an enumeration type, or be able to be implicitly converted to these types.

Here is an example declaration of a variable in an if clause

 #include <iostream> #include <string> class A { private: std::string s; public: explicit A( const std::string &s ) : s( s ) { } explicit operator bool() const { return s == "Hi"; } void hi() { std::cout << "I'm glad to see you" << std::endl; } void bye() { std::cout << "See you latter" << std::endl; } }; int main() { if ( A a { "Hi" } ) { a.hi(); } else { a.bye(); } if ( A a { "Bye" } ) { a.hi(); } else { a.bye(); } } 

Console output

 I'm glad to see you See you latter 

In this example, a class is declared that has an explicit conversion operator for type bool . Therefore, in the condition of the If clause, you can declare a class object that, thanks to this operator, will be converted to a value of type bool . This object will be limited to the scope of the if .

    In c ++ 17, it became possible to set the declaration of a variable directly in a conditional if or switch . For example:

     if (int i = 42; cond) { ... } 
     switch (SomeType t; var) { ... } 

    The key point here is that branching is done on cond or var , not on i or t .

    Before c ++ 17, for these purposes we had to enter a new block through { } and it looked not very nice:

     { int i = 42; if (cond) { ... } } 

    The new syntax simulates just such an approach.