scoped: simplify universal references

this avoids problems with use after stack unwind discovered using
sanitizers.
This commit is contained in:
Danny Robson 2018-06-18 13:51:59 +10:00
parent db90a4db53
commit d74fca4e98

View File

@ -16,6 +16,8 @@
#pragma once #pragma once
#include <functional>
namespace util::scoped { namespace util::scoped {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -78,12 +80,8 @@ namespace util::scoped {
template <typename FuncT, typename ...Args> template <typename FuncT, typename ...Args>
class function { class function {
public: public:
explicit function (FuncT &&_function, Args&& ..._args) explicit function (FuncT &&_function, Args&& ..._args):
noexcept ( m_function (std::forward<FuncT> (_function)),
std::is_nothrow_move_constructible_v<FuncT> &&
std::is_nothrow_move_constructible_v<std::tuple<Args...>>
):
m_function (std::move (_function)),
m_args (std::forward<Args> (_args)...) m_args (std::forward<Args> (_args)...)
{ ; } { ; }
@ -93,7 +91,7 @@ namespace util::scoped {
~function () ~function ()
{ {
if (m_enabled) if (m_enabled)
std::apply (std::move (m_function), std::move (m_args)); std::apply (m_function, m_args);
} }
void disable (void) { m_enabled = false; } void disable (void) { m_enabled = false; }
@ -107,5 +105,5 @@ namespace util::scoped {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <typename FuncT, typename ...Args> template <typename FuncT, typename ...Args>
function (FuncT &&, Args&&...) -> function<FuncT&&,Args&&...>; function (FuncT,Args...) -> function<FuncT,Args...>;
}; };