From 7f08cf83aa5db58bfac004dddae565e6536eeb2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 15 Feb 2017 01:02:27 +0100 Subject: Little scopeguard cleanup, and add a test --- louloulibs/utils/encoding.cpp | 4 ++-- louloulibs/utils/scopeguard.hpp | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'louloulibs') diff --git a/louloulibs/utils/encoding.cpp b/louloulibs/utils/encoding.cpp index 087095f..aa91dac 100644 --- a/louloulibs/utils/encoding.cpp +++ b/louloulibs/utils/encoding.cpp @@ -152,7 +152,7 @@ namespace utils throw std::runtime_error("Cannot convert into UTF-8"); // Make sure cd is always closed when we leave this function - const auto sg = utils::make_scope_guard([&cd](auto&&){ iconv_close(cd); }); + const auto sg = utils::make_scope_guard([&cd](){ iconv_close(cd); }); size_t inbytesleft = str.size(); @@ -169,7 +169,7 @@ namespace utils char* outbuf_ptr = outbuf; // Make sure outbuf is always deleted when we leave this function - const auto sg2 = utils::make_scope_guard([outbuf](auto&&){ delete[] outbuf; }); + const auto sg2 = utils::make_scope_guard([outbuf](){ delete[] outbuf; }); bool done = false; while (done == false) diff --git a/louloulibs/utils/scopeguard.hpp b/louloulibs/utils/scopeguard.hpp index cd0e89e..e697fc3 100644 --- a/louloulibs/utils/scopeguard.hpp +++ b/louloulibs/utils/scopeguard.hpp @@ -87,9 +87,11 @@ private: }; template -auto make_scope_guard(F&& f) +auto make_scope_guard(F f) { - return std::unique_ptr>{(void*)1, std::forward(f)}; + static struct Empty {} empty; + auto deleter = [f = std::move(f)](Empty*) { f(); }; + return std::unique_ptr{&empty, std::move(deleter)}; } } -- cgit v1.2.3