Skip to content
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

Unclear design when connecting a functor object with operator() #9

Closed
TheWisp opened this issue Jan 23, 2020 · 1 comment
Closed

Unclear design when connecting a functor object with operator() #9

TheWisp opened this issue Jan 23, 2020 · 1 comment

Comments

@TheWisp
Copy link
Owner

TheWisp commented Jan 23, 2020

There is no way to distinguish between a functor and a lambda.

A functor may be treated the same way as a lambda and get copied into the signal. As a result, the copy gets notified instead of the functor object. This is fine if all the members are pointer or references or the operator only causes global side-effects. However, if we rely on the data in the functor, the result is wrong.

struct stuff{
    int x = 0;
    void operator()(int y) { x+=y; }
};

stuff s;
sig.connect(s);
sig(10);
cout << s.x; //prints 0 instead of 10

On the other hand, if we treat the functor as a regular object, it may go out of bounds without disconnecting, dangling the callback. In case of a lambda, there is actually no way to save the connection into the structure for an RAII cleanup.

@TheWisp
Copy link
Owner Author

TheWisp commented Jan 27, 2020

l-value functors or lambdas are now stored as references, whereas r-value ones are by value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant