From d51231a7a7dbbd56ff6680ea509e84a41eddfa2c Mon Sep 17 00:00:00 2001 From: Borislav Stanimirov Date: Tue, 24 Sep 2024 08:19:42 +0300 Subject: [PATCH] [ufunction] allow bind to copies of src funcs --- include/itlib/ufunction.hpp | 7 ++++--- test/t-ufunction-11.cpp | 14 +++++++++++++- test/t-ufunction-14.cpp | 6 ++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/itlib/ufunction.hpp b/include/itlib/ufunction.hpp index 81e7b9a..8bfac98 100644 --- a/include/itlib/ufunction.hpp +++ b/include/itlib/ufunction.hpp @@ -1,4 +1,4 @@ -// itlib-ufunction v1.01 +// itlib-ufunction v1.02 // // Unique Function // Non-copyable and noexcept move-constructible replacement for std::function @@ -30,6 +30,7 @@ // // VERSION HISTORY // +// 1.02 (2024-09-24) Allow binding to copies of source functions as per C++23 // 1.01 (2022-09-23) Allow ufunction from a free function // 1.00 (2020-10-15) Initial release // @@ -88,10 +89,10 @@ class ufunction : private std::function ufunction& operator=(ufunction&&) noexcept = default; template - ufunction(FO&& f) noexcept : function(copy_wrapper{std::move(f)}) {} + ufunction(FO f) noexcept : function(copy_wrapper{std::move(f)}) {} template - ufunction& operator=(FO&& f) noexcept + ufunction& operator=(FO f) noexcept { function::operator=(copy_wrapper{std::move(f)}); return *this; diff --git a/test/t-ufunction-11.cpp b/test/t-ufunction-11.cpp index 4ef8cd4..378f286 100644 --- a/test/t-ufunction-11.cpp +++ b/test/t-ufunction-11.cpp @@ -8,6 +8,9 @@ TEST_SUITE_BEGIN("ufunction"); +static_assert(!std::is_copy_constructible>::value, "must not be copy constructible"); +static_assert(!std::is_copy_assignable>::value, "must not be copy assignable"); + struct fnocopy { fnocopy() = default; @@ -22,7 +25,7 @@ struct fnocopy other.owner = false; return *this; } - int operator()(int n) { return n+5;} + int operator()(int n) { return n+5; } bool owner = true; }; @@ -52,3 +55,12 @@ TEST_CASE("Free func") func = sum; CHECK(func(3, 4) == 7); } + +TEST_CASE("from copy") +{ + auto func = [](int a, int b) { return a + b; }; + itlib::ufunction f(func); + itlib::ufunction f2(func); + CHECK(f(1, 2) == 3); + CHECK(f2(10, 20) == 30); +} \ No newline at end of file diff --git a/test/t-ufunction-14.cpp b/test/t-ufunction-14.cpp index 5885d9d..e276034 100644 --- a/test/t-ufunction-14.cpp +++ b/test/t-ufunction-14.cpp @@ -14,14 +14,12 @@ TEST_CASE("Basic") { using namespace itlib; - auto uptr = std::make_unique(53); - ufunction func([u = std::move(uptr)](){ + ufunction func([u = std::make_unique(53)](){ CHECK(*u == 53); }); func(); - auto uptr2 = std::make_unique(102); - func = [u = std::move(uptr2)]() { + func = [u = std::make_unique(102)]() { CHECK(*u == 102); }; func();