vendredi 9 août 2019

How do I use "else-if" with initializer in C++17?

From the cpp reference: https://en.cppreference.com/w/cpp/language/if

It does not seem that I can do this:

if (cond)
{}
else if (init; cond)  // <<--- init not allowed with "else if"
{}

I got around it in a rather silly way:

if (cond)
{}
else if ([]() -> bool
{
    init;
    if (cond)
    {
        // Do something in the same scope as 'init'
        return true;
    }
    return false;
}())
{}

Am I missing something obvious here about how to do this "correctly" using C++17?

Thanks!

Aucun commentaire:

Enregistrer un commentaire