itlib icon indicating copy to clipboard operation
itlib copied to clipboard

[ufunction] Can't construct ufunction from a free function

Open iboB opened this issue 2 years ago • 1 comments

This works with msvc, but with clang and gcc it produces:

[build] itlib/include/itlib/ufunction.hpp:106:63: error: invalid use of ‘const_cast’ with type ‘int (&)(int, int)’, which is a pointer or reference to a function type
[build]   106 |         copy_wrapper(const copy_wrapper& other) : func_object(const_cast<FO&&>(other.func_object)) { throw 0; } // should never get to here
[build]       |                                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Apparently the const_cast fails. However we need this const cast for the fake copy constructor.

Potential solutions would be:

  1. Instead of a const cast add a function which works with const_cast for non-free functions and just copies free functions
  2. Devise some other way of obtaining a FO instance... something like a code-level declval

The key is that this only needs to compile. It's not important to do something which does sensible stuff when executed.

Not sure how to approach this yet.

iboB avatar Dec 15 '21 03:12 iboB

Simple repro which can be added to t-ufunction-11.cpp:

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

TEST_CASE("Func")
{
    itlib::ufunction<int(int,int)> func = sum;
}

iboB avatar Dec 15 '21 03:12 iboB