itlib
itlib copied to clipboard
[ufunction] Can't construct ufunction from a free function
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:
- Instead of a const cast add a function which works with const_cast for non-free functions and just copies free functions
- Devise some other way of obtaining a
FO
instance... something like a code-leveldeclval
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.
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;
}