Hacker News new | ask | show | jobs
by i_am_a_peasant 464 days ago
it's not that weird to explicitly limit the scope of certain variables that logically belong together.

But i agree the C++ if(init;cond) thing was new to me.

1 comments

Like everything in C++, it has it's share of footguns

    if (Foo* f = GetPtr(); f->HasValue()) {} // wrong; f can be null
vs

    if (Foo* f = GetPtr(); f && f->HasValue()){}
Is probably the biggest pitfall. Especially if you're used to this:

    if (Foo* f = GetPtr())
    {
        f->DoTheThing(); // this is perfectly safe.
    }