Skip to content

Commit

Permalink
Implicit conversion from unique_function<R(Args...) const> to `uniq…
Browse files Browse the repository at this point in the history
…ue_function<R(Args...)>`
  • Loading branch information
LesleyLai committed Oct 31, 2019
1 parent 40e6be1 commit b6f1b2a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 249 deletions.
10 changes: 8 additions & 2 deletions include/unique_function.hpp
Expand Up @@ -180,14 +180,20 @@ template <typename R, typename... Args> struct unique_function_base {
template <typename R, typename... Args>
class unique_function<R(Args...)>
: public detail::unique_function_base<R, Args...> {
using base_type = detail::unique_function_base<R, Args...>;

public:
unique_function() = default;

template <typename Func, class DFunc = std::decay_t<Func>,
class = std::enable_if_t<!std::is_same_v<DFunc, unique_function> &&
std::is_move_constructible_v<DFunc>>>
explicit unique_function(Func&& func)
: detail::unique_function_base<R, Args...>{std::forward<Func>(func)}
explicit unique_function(Func&& func) : base_type{std::forward<Func>(func)}
{
}

unique_function(unique_function<R(Args...) const>&& other)
: base_type{static_cast<base_type&&>(other)}
{
}

Expand Down
13 changes: 11 additions & 2 deletions test/unique_function_test.cpp
Expand Up @@ -240,6 +240,15 @@ TEST_CASE("const unique_function")
{
// beyond::unique_function<int() const> f2{[]() mutable { return 42; }};

beyond::unique_function<int() const> f{[]() { return 42; }};
REQUIRE(f() == 42);
SECTION("A unique_function to const function")
{
beyond::unique_function<int() const> f{[]() { return 42; }};
REQUIRE(f() == 42);

SECTION("const specialization can implicit convert to none-const version")
{
beyond::unique_function<int()> f2 = std::move(f);
REQUIRE(f2() == 42);
}
}
}
245 changes: 0 additions & 245 deletions test/unique_function_test.cpp.autosave

This file was deleted.

0 comments on commit b6f1b2a

Please sign in to comment.