Skip to content

[libc++] Make expected trivially assignable whenever possible #47

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Quuxplusone
Copy link
Owner

Since @huixie90 asked for a proof of concept — here it is!

Note that =deleted functions don't need to be hidden from ABI, nor do they need to be constexpr or noexcept or have any particular return type. Note that =defaulted functions shouldn't be explicitly marked constexpr or noexcept.

Comment on lines +671 to +675
_LIBCPP_HIDE_FROM_ABI constexpr expected& operator=(const expected& __rhs)
requires __expected_have_copy_assignment<_Tp, _Err> &&
is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_assignable_v<_Tp> &&
is_trivially_copy_constructible_v<_Err> && is_trivially_copy_assignable_v<_Err>
= default;

Choose a reason for hiding this comment

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

Is it intended not to consider is_trivially_destructible_v? (See also LWG4026 and P0602R4 which considered trivial destructibility.)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Today, is_trivially_copy_constructible_v<T> logically implies is_trivially_destructible_v<T>. (Godbolt.) There is some controversy about the desirability of this fact, but it's still a fact. So my personal view is that we shouldn't add redundant conditions that won't ever affect the conjunction's result. But I know that "reasonable people disagree," and I certainly wouldn't object if this were upstreamed with the redundant condition added.


Actually, in Hagenberg this very question came up during discussion of P2786's is_nothrow_relocatable, where the version approved in plenary has ([meta.unary.prop]):

is_trivially_relocatable_v<T> || (is_nothrow_move_constructible_v<remove_all_extents_t<T>> &&
                                  is_nothrow_destructible_v<remove_all_extents_t<T>>)

Today, is_nothrow_move_constructible_v<remove_all_extents_t<T>> logically implies is_nothrow_destructible_v<remove_all_extents_t<T>>, so personally I wouldn't have written it this way. But a vocal contingent on LWG did want the redundant condition, and since it has no effect there's no physical reason to oppose adding it, so, it got added.

Choose a reason for hiding this comment

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

Today, is_trivially_copy_constructible_v<T> logically implies is_trivially_destructible_v<T>.

Oh, I think this is arguable, as LWG2116 and LWG2827 aren't resolved yet.

I guess it's worthwhile to explicitly specify that multiple is_trivially_* imply is_trivially_destructible_v (or at least having a trivial non-deleted destructor), as we can hardly do anything special for a trivially-meowable but not trivially destructible class.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Today, is_trivially_copy_constructible_v<T> logically implies is_trivially_destructible_v<T>.

Oh, I think this is arguable, as LWG2116 and LWG2827 aren't resolved yet.

Sure, but LWG did vote in 2022 to close them as NAD/evolutionary (as shown in the minutes for LWG2116); I'm not sure why they haven't been actually closed in the issue system yet.

The evolutionary question is indeed argued. But I don't think such a change stands much chance precisely because there's already code just like the code I wrote, out there in the wild, which would quietly change semantics if we changed the meaning of these traits. In fact, notice that there exists a paper discussing why we might want to change the semantics of these traits (P2842R0, cplusplus/papers#1547 ), and its proposed resolution (§7 and §9) is literally to ratify LWG's position and close the issues as NAD — after all that it's still not proposing that we should change the traits!

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.

2 participants