-
Notifications
You must be signed in to change notification settings - Fork 7
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
No CTAD for lambdas & function objects #4
Comments
It is temporarily by design. You may note that
These issues apply to In contrast, the CTAD on My feeling is that, CTAD on |
Thanks for the explanation. Some context & motivation: I noticed that quite often I try to write auto handler1 = [&](){};
auto handler2 = [&](){};
auto handler = condition?
handler1 :
handler2;
// Call handler more than once This obviously does not work, because Type-erasing them with auto handler = condition?
std::function(handler1) :
std::function(handler2);
but obviously has an extra cost.
auto handler = condition?
std23::function_ref<void()>(handler1) :
std23::function_ref<void()>(handler2); |
If it is okay to spell the type once, maybe auto handler = [&] -> std23::function_ref<void()> {
if (cond)
return handler1;
else
return handler2;
}(); https://godbolt.org/z/3r5Pqsdnr If you definitely want some form of deduction, a weird trick is auto handler =
cond ? std23::function_ref(
std23::nontype<&decltype(handler1)::operator()>, handler1)
: std23::function_ref(
std23::nontype<&decltype(handler2)::operator()>, handler2); https://godbolt.org/z/bxhv45q4h The trick can be an approach if you ditch struct Handler1 {
void fn() {}
} handler1;
struct Handler2 {
void fn() {}
} handler2;
auto handler =
cond ? std23::function_ref(std23::nontype<&Handler1::fn>, handler1)
: std23::function_ref(std23::nontype<&Handler2::fn>, handler2); |
There are ways of course, but my goal was to reduce the typing and repeating myself, not to get some form of deduction just for the sake of it :)
Should it though?
CTAD, just like |
I can argue for
and there can be |
Compile the code:
Expected
Successful compilation in all 3 cases, same as
std::function
above.Actual
Only the first case works.
2nd:
3rd:
Compiler Explorer:
https://godbolt.org/z/rsc1ndjoh
Is it by design / overlook? As mentioned,
std::function
supports all these cases.The text was updated successfully, but these errors were encountered: