0

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;
6
  • 8
    Use 2 statements.
    – prapin
    Commented Sep 19, 2022 at 13:21
  • 2
    @prapin and some {} to be pedantic Commented Sep 19, 2022 at 13:25
  • One way to think of C++17 is that it is a new, separate, improved language distinct from its predecessor C++14. Most-or-all C++14-isms will work in C++17 (since backwards compatibility is striven to be maintained as much as able), but little-or-none of C++17-isms will work in C++14.
    – Eljay
    Commented Sep 19, 2022 at 13:27
  • 1
    You probably want dynamic_cast here instead Commented Sep 19, 2022 at 13:30
  • 1
    @TheDreamsWind bold claim without larger context.
    – Marek R
    Commented Sep 19, 2022 at 14:06

2 Answers 2

10

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->UserState())
        return;
}
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.