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

Fix flatten optional assignment #142

Merged
merged 4 commits into from
Dec 13, 2023
Merged

Conversation

tcbrindle
Copy link
Owner

The single-pass version of flatten_adaptor currently does a move-assignment of an optional, which doesn't work if the inner sequence is not move-assignable -- for example if it involves a capturing lambda.

This is rare, but I ran into it today doing Advent of code...

This PR fixes it in two ways. Firstly, in our optional<T> implementation, if T is copy-constructible but not copy-assignable, we destroy + construct in the assignment operator (and likewise for move). This allows optional<T> to be copy/move-assignable even when T is not, for example if it's a capturing lambda.

Secondly, we actually remove the problem by using optional::emplace in flatten_adaptor, which results in fewer total operations anyway, but does require adding optional<T&>::emplace so things don't break.

The single-pass version of flatten_adaptor currently does a move-assignment of an optional, which doesn't work if the inner sequence is not move-assignable -- for example if it involves a capturing lambda.

This is rare, but I ran into it today doing Advent of code...
For copy/move assignment between two engaged optionals, std::optional always use's T's assignment operator.

For flux::optional, if T is not copy/move assignable, then we fall back to destroying the contained LHS and copy/move constructing from the RHS.

This was part of my justification for providing optional references back in the Flow days, and it's still appropriate today.
We've had optional<T>::emplace for a while. As you might expect, it constructs a T directly inside the optional, using the supplied arguments.

For the optional<T&> version, we require the argument to be a single argument of a "compatible" lvalue reference type -- that is, the same condition as the constructor.

This allows emplace to be used in generic code which doesn't know if it's dealing with optional<T> or <T&>.
Rather than constructing a RHS optional and using assignment, we'll use emplace() to construct the object directly in the LHS, which results in fewer operations.

It also means the changes to the optional assignement operators we're necessary, but never mind...
Copy link

codecov bot commented Dec 13, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (2677b03) 97.98% compared to head (e999e7c) 97.98%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #142   +/-   ##
=======================================
  Coverage   97.98%   97.98%           
=======================================
  Files          66       66           
  Lines        2380     2383    +3     
=======================================
+ Hits         2332     2335    +3     
  Misses         48       48           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tcbrindle tcbrindle merged commit 9adb7f4 into main Dec 13, 2023
27 checks passed
@tcbrindle tcbrindle deleted the pr/fix-flatten-optional-assignment branch January 20, 2024 12:29
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.

1 participant