From 0c6b245d972a953f32dc2a076dc300d4a6a8f229 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Mon, 13 Jul 2020 15:31:21 +0300 Subject: [PATCH] [base] ExceptionCatcher. Prevent returning reference on a local temporary object. --- base/exception.hpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/base/exception.hpp b/base/exception.hpp index 5ea31cb0e0..ab27cfc611 100644 --- a/base/exception.hpp +++ b/base/exception.hpp @@ -25,8 +25,9 @@ private: }; template -std::result_of_t ExceptionCatcher( - std::string const & comment, bool & exceptionWasThrown, Fn && fn, Args &&... args) noexcept +std::result_of_t ExceptionCatcher(std::string const & comment, + bool & exceptionWasThrown, Fn && fn, + Args &&... args) noexcept { try { @@ -47,7 +48,12 @@ std::result_of_t ExceptionCatcher( } exceptionWasThrown = true; - return std::result_of_t(); + using ReturnType = std::decay_t>; + if constexpr (!std::is_same_v) + { + static const ReturnType defaultResult = {}; + return defaultResult; + } } #define DECLARE_EXCEPTION(exception_name, base_exception) \