-
-
Notifications
You must be signed in to change notification settings - Fork 912
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
lambda support for signalling #132
Comments
The signal stuff is made to be fast. It’s designed to avoid allocations. If you want a lambda with state, you have to store the state somewhere. If your lambda is state-less then it can be cast to a function pointer. Unary plus operator can do that. signal.sink().connect<+[] (int,char) {
/* ... */
}>(); I’m not sure how you would go about Although, we could make the user in charge of the state. Maybe if the user passes in a pointer to an object, we could grab a pointer to S instance{x};
signal.sink().connect<S>(&instance); All we can really do is make the workaround in the original post a little cleaner. |
What @Kerndog73 said is more or less what I'd have said, but for the fact that lambdas cannot be used as template parameters in any case as far as I remember. The fact is that If you want to create a signal class that accepts lambdas, probably |
Oh, of course lambdas can’t be used as template parameters! Every lambda expression has a different type. It just wouldn’t make sense. Silly me! I should have compiled that. |
I'm closing the issue because it's invalid, something we cannot implement within |
Don't worry it's not vital, wondered if it was overlooked. 👍 @Kerndog73 for the +[] operator |
I understand signals aren't in charge of the lifetime of the listener, however i don't think writing bool thing_happened = false;
auto listener = [&thing_happened]{
thing_happened = true;
};
sink.connect<&decltype(listener)::operator()>(&listener); is quite nice. A possible alternative would be to add an overload of template <typename Listener>
connection connect(Listener* listener) {
connect<&Listener::operator()>(listener);
} This would allow rewriting the first snippet as bool thing_happened = false;
auto listener = [&thing_happened]{
thing_happened = true;
};
sink.connect(&listener); which seems a lot nicer in my opinion. |
@Snektron I was thinking about this just the other day! |
@Snektron @Kerndog73 a lambda created on the stack and attached this way would result in a dangling pointer and therefore a crash. |
Wouldn't the same thing happen with a listener created on the stack? |
@Snektron indeed, of course. @Kerndog73 correctly commented the decisions behind
The fact is that the |
This syntax especially
Would be very useful to support lambda syntax (c++ converts to the very same pattern internally, I think)
The text was updated successfully, but these errors were encountered: