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

Short-circuit some variant constraints #4966

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

Conversation

CaseyCarter
Copy link
Member

variant's converting constructor and assignment operator templates are constrained to reject arguments of the variant's type. In such a case, the templates instantiated to check the constructibility constraint might be ill-formed outside the immediate context of template instantiation causing a hard error. We should split the constraints into multiple enable_if_ts to enable short-circuiting of later constraints when the earlier constraints fail.

Fixes #4959.

`variant`'s converting constructor and assignment operator templates are constrained to reject arguments of the `variant`'s type. In such a case, the templates instantiated to check the constructibility constraint might be ill-formed outside the immediate context of template instantiation causing a hard error. We should split the constraints into multiple `enable_if_t`s to enable short-circuiting of later constraints when the earlier constraints fail.

Fixes microsoft#4959.
@CaseyCarter CaseyCarter added the bug Something isn't working label Sep 19, 2024
@CaseyCarter CaseyCarter requested a review from a team as a code owner September 19, 2024 04:54
&& !_Is_specialization_v<_Remove_cvref_t<_Ty>, in_place_type_t>
&& !_Is_in_place_index_specialization<_Remove_cvref_t<_Ty>>,
int> = 0,
enable_if_t<is_constructible_v<_Variant_init_type<_Ty, _Types...>, _Ty>, int> = 0>
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm open to suggestions on improving the formatting of these enable_if_ts. Nothing I tried looked significantly better than what clang-format produces.

Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to stop it from aligning the = 0s? That's the only somewhat ugly thing going on here.

Copy link
Member Author

@CaseyCarter CaseyCarter Sep 19, 2024

Choose a reason for hiding this comment

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

Other than clang-format off, our normal trick is to add a blank line breaking consecutive lines with = so clang-format won't align. Would a weird blank line in the middle of the template head be better or worse:

        enable_if_t<sizeof...(_Types) != 0 && !is_same_v<_Remove_cvref_t<_Ty>, variant>
                        && !_Is_specialization_v<_Remove_cvref_t<_Ty>, in_place_type_t>
                        && !_Is_in_place_index_specialization<_Remove_cvref_t<_Ty>>,
            int> = 0,

        enable_if_t<is_constructible_v<_Variant_init_type<_Ty, _Types...>, _Ty>, int> = 0>

Copy link
Member

Choose a reason for hiding this comment

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

I like the idea of having a blank line here, it seems to be the least of all evils and it kind of helps with the readability of the bulky SFINAE conditions.

@StephanTLavavej StephanTLavavej self-assigned this Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Final Review
Development

Successfully merging this pull request may close these issues.

P0608R3 breaks flang build with Clang
3 participants