Home > Net >  Convert if with init-statement (c 17) to c 14
Convert if with init-statement (c 17) to c 14

Time:09-20

This works only for c 17. Is there a way to convert this to c 14?

if (auto user = static_cast<CUser*>(pMover); user && !user->UserState())
        return;

CodePudding user response:

You have to split the if into 2 statements.
In order to limit the scope of user to the if statement, you can enclose it with {...}:

{
    auto user = static_cast<CUser*>(pMover);
    if (user && !user->IsRecordBookOptIn())
        return;
}

CodePudding user response:

In this case, it's simple:

if (pMover && static_cast<CUser*>(pMover)->IsRecordBookOptIn()) return;

static_cast<CUser*>(pMover) is nullptr if and only if pMover is nullptr. And in that case, && doesn't evaluate the right-hand side.

  • Related