Skip to content

Commit

Permalink
[ufunction] Allow ufunction from a free function. Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
iboB committed Sep 23, 2022
1 parent 25ad1bf commit 2abc53f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/itlib/ufunction.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// itlib-ufunction v1.00
// itlib-ufunction v1.01
//
// Unique Function
// Non-copyable and noexcept move-constructible replacement for std::function
Expand Down Expand Up @@ -30,6 +30,7 @@
//
// VERSION HISTORY
//
// 1.01 (2022-09-23) Allow ufunction from a free function
// 1.00 (2020-10-15) Initial release
//
//
Expand Down Expand Up @@ -96,6 +97,15 @@ class ufunction : private std::function<F>
return *this;
}

// function pointer overloads (otherwise clang and gcc complain for const_cast of function pointers)
// noexcept since we're relying on small function opti
ufunction(F* fptr) noexcept : function(fptr) {}
ufunction& operator=(F* fptr) noexcept
{
function::operator=(fptr);
return *this;
}

using function::operator bool;
using function::operator();
private:
Expand Down
11 changes: 11 additions & 0 deletions test/t-ufunction-11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ TEST_CASE("Basic")
CHECK(f2(1) == 6);
}

int sum(int a, int b) { return a + b; }

TEST_CASE("Free func")
{
itlib::ufunction<int(int, int)> func = sum;
CHECK(func(1, 2) == 3);
func = [](int a, int b) { return a * b; };
CHECK(func(3, 4) == 12);
func = sum;
CHECK(func(3, 4) == 7);
}

0 comments on commit 2abc53f

Please sign in to comment.