Naios / function2

Improved and configurable drop-in replacement to std::function that supports move only types, multiple overloads and more
http://naios.github.io/function2
Boost Software License 1.0
545 stars 46 forks source link

Constructing function_view from fu2::function #13

Closed dermojo closed 6 years ago

dermojo commented 6 years ago

@Naios


As a workaround for issue #12 I tried to use fu2::function_view instead of a const-ref to a fu2::unique_function. Implicit conversion doesn't work here (it does for std::function). Is this intentional?

Sample code (doesn't compile with GCC/Clang):

#include <function2/function2.hpp>
#include <iostream>

static void call(fu2::function_view<void()> fun)
{
   fun();
}

int main()
{
   fu2::unique_function<void()> fun = [] { std::cout << "Hello World\n"; };
   call(fun);
   return 0;
}

The error from Clang looks helpful:

$ clang-6.0 -std=c++17 -o fu2_view fu2_view.cpp -I function2-3.0.0/include/
fu2_view.cpp:12:4: error: no matching function for call to 'call'
   call(fun);
   ^~~~
fu2_view.cpp:4:13: note: candidate function not viable: no known conversion from 'function_base<true, false, [4 * ...]>' to
      'function_base<false, true, [4 * ...]>' for 1st argument
static void call(fu2::function_view<void()> fun)
            ^
1 error generated.
Naios commented 6 years ago

I will take a look into this, but it seems like this is a bug in the implementation. The non owning view should accept everything regardless the callable is copyable or not.

dermojo commented 6 years ago

Great, thanks!

Naios commented 6 years ago

Thanks for your bug report. It was fixed due to version v3.1.0.