samedi 25 novembre 2017

C++17 : Are 'Selection Statements with Initializer' broken?

Let's say I start with;

...
auto b = foo(a);
if (!a && (b < c))
{
    bar(b);
}
...

And I notice I can optimize it for C++17 using the nice new 'selection statements with initializer' to become;

...
if (!a && (auto b = foo(a); (b < c)))
{
    bar(b);
}
...

then, to my dismay, it doesn't compile, under GCC 7.2 Now this IS in the spirit of the optimization since it's kinda the point of the change to C++17, neater AND skips the local variable if the first test, '!a' fails which would be nice and neatly efficient.

However, if I change it to;

...
if (!a)
{
    if (auto b = foo(a); (b < c))
    {
        bar(b);
    }
}
...

then it works... BUT what's the point since this is simply;

...
if (!a)
{
    auto b = foo(a); 
    if (b < c)
    {
        bar(b);
    }
}
...

which seems to spoil the whole point of having the 'statement selection with initializer' in the first place.

Q. So is this feature of C++17 broken or useless fluff if it doesn't allow parts of conditions to have this feature and demands the ENTIRE 'if' carries it?

Aucun commentaire:

Enregistrer un commentaire