Simplify sol::optional<T&>::emplace #1648
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See comment on #1606: #1606 (comment)
The implementation for
emplace
in theoptional<T>
specialisation is needlessly complex, and thestatic_assert
is testing the wrong thing.Consider:
Now look at the
static_assert
– it’s testing thatT
can be constructed using the argument types passed toemplace
. However, theoptional<T&>
doesn’t holdT
, it logically holdsT&
(although it’s implemented usingT*
to allow assignment). The only thing thatT&
can be constructed with isT&
. It makes no sense to use variadic arguments.When called with
T&
, thestatic_assert
only succeeds by chance whenT
is copy constructible. It will fail if it isn’t, and it will succeed for other things thatT
can be constructed with when it shouldn’t.For example, the
static_assert
will succeed here, but it won’t work:Conversely, this won’t work when it should
Since you know
T*
is trivially destructible and copyable, you could implementemplace
as: