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

type_caster<std::optional>: support non-assignable types #358

Merged
merged 1 commit into from Nov 9, 2023

Conversation

WKarel
Copy link
Contributor

@WKarel WKarel commented Nov 9, 2023

So far, type_caster<std::optional>::from_python requires T to support copy-assignment, because it uses std::optional::operator=, which in turn uses

  • in-place construction of T if empty, or
  • copy-assignment of T if non-empty.

Removed this requirement by replacing std::optional::operator= with std::optional::emplace, which always uses in-place construction.

Apart from supporting non-assignable types, there should be no changes in behaviour, because type_caster<std::optional>::from_python is called only once on each instance, and so its member value is always empty, anyway.

My use case: bind functions with arguments of type std::optional<Eigen::Map>. Compilation fails without this change.

@wjakob
Copy link
Owner

wjakob commented Nov 9, 2023

Nice, thank you!

@wjakob wjakob merged commit 0c9b648 into wjakob:master Nov 9, 2023
21 checks passed
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.

None yet

2 participants