Move only callables#531
Conversation
|
Thanks for the PR. A few comments:
But I would perfectly imagine clients may need move-only semantics for other callback types, too, not only |
Yes, this is a good question. The reason is that without mutable, all captures are automatically #include <functional>
#include <iostream>
int main() {
std::move_only_function<void()> callback{
[] { std::cout << "Invoked!" << std::endl; }};
auto wrapper = [callback = std::move(callback)] { callback(); };
wrapper();
return 0;
}This does not compile because it tries to invoke the captured
I think you're right! I may have done too much there. I'll change that back. That's indeed unintentional.
There is a reason for only doing it for one-shot callbacks (which I mistakenly bungled up in my PR as you rightly pointed out!). The reason is that |
93e94ae to
52c1ca8
Compare
|
Thanks for more detailed elaboration. To the first point, I think I now understand, you want to achieve two things:
I was first confused by you mentioning that captures are moved, but I didn't see any explicit What I still don't quite get is why not make the other callback types Thanks a lot. |
|
I was totally unaware of the option that you can const-qualify the signature of the callback for an I'm OK with just using |
|
OK, after your latest comment and realizing that you can put qualifiers on the type in the
I also took the liberty of converting some |
e8a6c62 to
d284be0
Compare
|
@omartijn Thanks for your additional thoughts and work. I will look at it and come back. In the meantime I realized we don't have pipelines enabled on |
|
Nice! I'm definitely "team rebase", so not an issue. Saw some issues in the tests flagged by clang-tidy so fixed those too. Also disabled a few tests that probably should be fixed but they were pre-existing (likely not flagged with an older clang-tidy). Also: The tests run with a very out-dated c++ library it seems so that doesn't have |
6d80bce to
a3e6df1
Compare
There is no rules-file, and having clang-format enabled will have it use whatever the default rule-set is (I think the rules used for clang itself), causing massive changes on every save.
All callbacks are now std::move_only_functions. Internally, we allow them to be `const` where possible, so they can be invoked from const-qualified member functions. One-shot callbacks are marked so they can only be invoked on rvalue functions, which more clearly signifies that they have to be moved out to be invoked (and thus cannot be invoked again).
|
OK, I also realized there was a bug with running
Feel free to review. From my side I think we are good to go. I like your separation of callbacks and additional marking with |
a560ac0 to
61a139a
Compare
|
I must admit I'm still a bit hesitant about Furthermore, user can use const function also without Is there any issue you foresee (except that you are used to it with boost::asio) for sdbus-c++ users if we removed |
|
No - the |
59655fe
into
Kistler-Group:feat/release-v3.x
This PR:
std::move_only_functionfor one-shot callbacksmutablefor lambdas moving captures outThe lambdas made mutable were for different reasons. In a few spots, there were lambdas capturing something and then using
std::move()to move the capture out on invocation. That only works correctly if the lambdas is mutable, Without mutable, the capture isconstand you'll get aconst T&&, which is not what you want.Not all callbacks are converted to
std::move_only_functioneither. I only did that for "one-shot callbacks". In contrast tostd::function, the invocation operator ofstd::move_only_functionis not const-qualified. It may change internal state (and not be safe for repeated invocation).Clang-format I have disabled because there were no formatting rules, causing default rules to be automatically applied whenever I save a file. I do think it could be a good idea to settle on a particular formatting at some point.
Note that I didn't change any of the tests. That's not necessary, because it's OK to move an
std::functioninto anstd::move_only_function(but not the other way around). This shows that the changes are an ABI-break but not an API-break.