Defining a variable in the condition part of an if-statement?

前端 未结 5 454
夕颜
夕颜 2020-12-13 12:14

I was just shocked, that this is allowed:

if( int* x = new int( 20 ) )
{
    std::cout << *x << \"!\\n\";
    // delete x;
}
else
{
    std::cout         


        
相关标签:
5条回答
  • 2020-12-13 12:19

    Not really an answer (but comments are not well suited to code samples), more a reason why it's incredibly handy:

    if (int* x = f()) {
        std::cout << *x << "\n";
    }
    

    Whenever an API returns an "option" type (which also happens to have a boolean conversion available), this type of construct can be leveraged so that the variable is only accessible within a context where it is sensible to use its value. It's a really powerful idiom.

    0 讨论(0)
  • 2020-12-13 12:21

    Here is an example demonstrating non typical use of a variable declared in an if condition.

    Type of variable is int & which is both convertible to boolean and useable in the then and else branches.

    #include <string>
    #include <map>
    #include <vector>
    using namespace std;
    
    vector<string> names {"john", "john", "jack", "john", "jack"};
    names.push_back("bill"); // without this push_back, my g++ generated exe fails :-(
    map<string, int> ages;
    int babies = 0;
    for (const auto & name : names) {
        if (int & age = ages[name]) {
            cout << name << " is already " << age++ << " year-old" << endl;
        } else {
            cout << name << " was just born as baby #" << ++babies << endl;
            ++age;
        }
    }
    

    output is

    john was just born as baby #1
    john is already 1 year-old
    jack was just born as baby #2
    john is already 2 year-old
    jack is already 1 year-old
    bill was just born as baby #3
    

    Unfortunately, the variable in the condition may only be declared with the '=' declaration syntax.

    This rules out other possibly useful cases of types with an explicit constructor.

    For instance, next example using an std::ifstream won't compile ...

    if (std::ifstream is ("c:/tmp/input1.txt")) { // won't compile!
        std::cout << "true: " << is.rdbuf();
    } else {
        is.open("c:/tmp/input2.txt");
        std::cout << "false: " << is.rdbuf();
    }
    

    Edited january 2019 ... you now can emulate what I explained could not be done ...

    This works for moveable classes like ifstream in C++11 and even for non copiable classes since C++17 with copy elision.

    Edited May 2019: use auto to alleviate verbosity

    {
        if (auto is = std::ifstream ("missing.txt")) { // ok now !
            std::cout << "true: " << is.rdbuf();
        } else {
            is.open("main.cpp");
            std::cout << "false: " << is.rdbuf();
        }
    }
    struct NoCpy {
        int i;
        int j;
        NoCpy(int ii = 0, int jj = 0) : i (ii), j (jj) {}
        NoCpy(NoCpy&) = delete;
        NoCpy(NoCpy&&) = delete;
        operator bool() const {return i == j;}
        friend std::ostream & operator << (std::ostream & os, const NoCpy & x) {
            return os << "(" << x.i << ", " << x.j << ")";
        }
    };
    {
        auto x = NoCpy(); // ok compiles
        // auto y = x; // does not compile
        if (auto nocpy = NoCpy (7, 8)) {
            std::cout << "true: " << nocpy << std::endl;
        } else {
            std::cout << "false: " << nocpy << std::endl;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 12:22

    Definition of a variable in the conditional part of a while, if, and switch statement are standard. The relevant clause is 6.4 [stmt.select] paragraph 1 which defines the syntax for the condition.

    BTW, your use is pointless: if new fails it throws a std::bad_alloc exception.

    0 讨论(0)
  • 2020-12-13 12:23

    This is allowed by the specification, since C++98.

    From Section 6.4 "Selection statements":

    A name introduced by a declaration in a condition (either introduced by the type-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.

    The following example is from the same section:

    if (int x = f()) {
        int x;    // ill-formed, redeclaration of x
    }
    else {
        int x;    // ill-formed, redeclaration of x
    }
    
    0 讨论(0)
  • 2020-12-13 12:23

    It is standard, even in the old C++ 98 version of the language:

    enter image description here

    0 讨论(0)
提交回复
热议问题