1

I'm using Qt 5.15.0. I have a try-catch block as follows:

try
{
   //some code here
}
catch(std::exception& e)
{
   QTimer::singleShot(0,[e](){ throw e;});
}

I get a warning treated as an error C4702: QtCore\qobjectdefs_impl.h(146): warning C4702: unreachable code. Any idea what's going wrong? Removing the e from the capture and leaving the lambda empty fixes the problem, so looks like it's something related to that? Thank you

blondbeer
  • 45
  • 4

1 Answers1

1

This is basically noise and you can ignore that warning. It comes from how Qt wraps your functor. The code around the error location looks as follows:

template <typename, typename, typename, typename> struct FunctorCall;
template <int... II, typename... SignalArgs, typename R, typename Function>
struct FunctorCall<IndexesList<II...>, List<SignalArgs...>, R, Function> {
    static void call(Function &f, void **arg) {
        f((*reinterpret_cast<typename RemoveRef<SignalArgs>::Type *>(arg[II+1]))...), ApplyReturnValue<R>(arg[0]);
    }
};

The compiler is complaining that the ApplyReturnValue<R>(arg[0]) expression won't be executed. It's a valid complaint, but that code does not have to run, so there's nothing to worry about, because in your case ApplyReturnValue<R> is really ApplyReturnValue<void> since the lambda has no return value, and thus the overridden operator, does nothing:

template<typename T>
    void operator,(T, const ApplyReturnValue<void> &) {}

So you can selectively disable that warning for that bit of code only, ie.:

try
{
   // some code here
}
catch(std::exception& e)
{
   #pragma warning(push)
   #pragma warning(disable : 4702)
   QTimer::singleShot(0,[e](){ throw e;});
   #pragma warning(pop)
}

And also, if you know that the thread you're running on is the main thread, you could use QMetaObject::invokeMethod(qApp, [e]{ throw e; }, Qt::QueuedConnection); instead of the zero timer, although admittedly it's more text, so I guess it's up to one's preference without a clearly better syntax.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313