-
-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FR: a constructor for delegate with a freestanding function #966
Comments
I'll take a look to see if it is possible. |
I've been doing C++ for over 20 years, and it still manages to surprise me sometimes, especially convoluted template meta-programming! |
I've had a go at this, and the fundamental problem is that you cannot explicitly declare the template parameters for a constructor. They must be deduced from the constructor's argument. The only way to simplify construction is to make a lambda or functor wrapper around the free function.
|
With optimisation, the resulting code is still very efficient, even with the lambda and delegate. For this code and -O1 optimisation, the lambda and delegate reduce to a direct call of the free function.
|
Hi, thanks for looking into this!
Do you know if it's possible to explicitly cast a function (pointer) to a type to make compiler deduce template parameters?
where
I tried to play with library code, but every time the compiler complained. However in terms of verbosity this already approaches existing solution with |
I had a play this morning with creating a
|
I have worked out three
|
So, compared to existing solution, this saves an extra template specialization, right?
|
Yes, that's correct. |
Ok. Doesn't completely solve it, but this will make using delegates easier! |
Actually, I've discovered that the syntax is only valid for C++17 and up. |
…opying/assigning from delegate with mismatching signature #966 A constructor for delegate with a freestanding function
…opying/assigning from delegate with mismatching signature #966 A constructor for delegate with a freestanding function
At the moment, you construct a delegate with a freestanding function like this:
my_delegate_type::create<my_function>()
, and use it like this:add_callback(my_delegate_type::create<my_function>())
.It would be nice to have an implicit constructor for this case, then this
::create
distraction could be omitted and simplified to justadd_callback(my_function)
.As I understand, this syntax is possible for functors and lambdas, but not for freestanding functions. There are probably obstructions to doing this, otherwise you'd probably have implemented it long ago, but my c++-fu is not strong enough to understand it.
The text was updated successfully, but these errors were encountered: