Skip to content

waitFor(QCoro::Task<T>): Do not require T to be default constructible#223

Merged
danvratil merged 6 commits intoqcoro:mainfrom
joesdiner:task-wait-for-non-default-constructable
Jul 14, 2024
Merged

waitFor(QCoro::Task<T>): Do not require T to be default constructible#223
danvratil merged 6 commits intoqcoro:mainfrom
joesdiner:task-wait-for-non-default-constructable

Conversation

@joesdiner
Copy link

A patch so that T no longer needs to be default constructible when calling QCoro::waitFor(QCoro::Task<T>). Not claiming that this is the best way to do things, but it worked for me. That said I would appreciate any additional testing. I'm only a light user of the QCoro library.

I ended up duplicating some code logic, because I'm not sure if using std::optional in all cases could decrease performance. Previously a T would always be default constructed, but then RVO should occur when returning the value from detail::waitFor() so no additional copy/move would occur. If the default constructor for T is cheap, this should have been fine.

With using a std::optional<T> though, Ts default constructor is never called, but I don't think RVO can occur anymore with returning its value from detail::waitFor(). I think T may be moved to return its value, but this is a corner of C++ I don't have a complete grasp on.

closes #222

Copy link
Collaborator

@danvratil danvratil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the patch! Could you please update the patch and drop the branch for default-constructible types and just always use std::optional? It actually is the more correct approach since we avoid the (potentially high) cost of calling the default constructor, which also avoids the triggering of any potential side-effects that the constructor might have.

Could you also please add a unit-test for a non-default-constructible type to make sure we don't regress in the future? Thank you

@joesdiner
Copy link
Author

Uses the std::optional paradigm now even T has a default constructor. Added some basic unit tests.

Copy link
Collaborator

@danvratil danvratil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MacOS CI is lazy today for some reason, but the change passes on Linux Clang and I don't have any reason to suspect AppleClang to not pass as well here.

Thanks a lot for the change and for adding the unittest 👍.

There's one more PR waiting, once it's merged I'll do a new QCoro release with those changes.

@danvratil danvratil merged commit 4c01291 into qcoro:main Jul 14, 2024
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

Successfully merging this pull request may close these issues.

waitFor(QCoro::Task<T>): Do not require T to be default constructible

2 participants