Skip to content
Snippets Groups Projects
  • Jonathan Wakely's avatar
    b72e7add
    libstdc++: Constrain equality ops for std::pair, std::tuple, std::variant · b72e7add
    Jonathan Wakely authored
    Implement the changes from P2944R3 which add constraints to the
    comparison operators of std::pair, std::tuple, and std::variant.
    
    The paper also changes std::optional, but we already constrain its
    comparisons using SFINAE on the return type. However, we need some
    additional constraints on the [optional.comp.with.t] operators that
    compare an optional with a value. The paper doesn't say to do that, but
    I think it's needed because otherwise when the comparison for two
    optional objects fails its constraints, the two overloads that are
    supposed to be for comparing to a non-optional become the best overload
    candidates, but are ambiguous (and we don't even get as far as checking
    the constraints for satisfaction). I reported LWG 4072 for this.
    
    The paper does not change std::expected, but probably should have done.
    I'll submit an LWG issue about that and implement it separately.
    
    Also add [[nodiscard]] to all these comparison operators.
    
    libstdc++-v3/ChangeLog:
    
    	* include/bits/stl_pair.h (operator==): Add constraint.
    	* include/bits/version.def (constrained_equality): Define.
    	* include/bits/version.h: Regenerate.
    	* include/std/optional: Define feature test macro.
    	(__optional_rep_op_t): Use is_convertible_v instead of
    	is_convertible.
    	* include/std/tuple: Define feature test macro.
    	(operator==, __tuple_cmp, operator<=>): Reimplement C++20
    	comparisons using lambdas. Add constraints.
    	* include/std/utility: Define feature test macro.
    	* include/std/variant: Define feature test macro.
    	(_VARIANT_RELATION_FUNCTION_TEMPLATE): Add constraints.
    	(variant): Remove unnecessary friend declarations for comparison
    	operators.
    	* testsuite/20_util/optional/relops/constrained.cc: New test.
    	* testsuite/20_util/pair/comparison_operators/constrained.cc:
    	New test.
    	* testsuite/20_util/tuple/comparison_operators/constrained.cc:
    	New test.
    	* testsuite/20_util/variant/relops/constrained.cc: New test.
    	* testsuite/20_util/tuple/comparison_operators/overloaded.cc:
    	Disable for C++20 and later.
    	* testsuite/20_util/tuple/comparison_operators/overloaded2.cc:
    	Remove dg-error line for target c++20.
    b72e7add
    History
    libstdc++: Constrain equality ops for std::pair, std::tuple, std::variant
    Jonathan Wakely authored
    Implement the changes from P2944R3 which add constraints to the
    comparison operators of std::pair, std::tuple, and std::variant.
    
    The paper also changes std::optional, but we already constrain its
    comparisons using SFINAE on the return type. However, we need some
    additional constraints on the [optional.comp.with.t] operators that
    compare an optional with a value. The paper doesn't say to do that, but
    I think it's needed because otherwise when the comparison for two
    optional objects fails its constraints, the two overloads that are
    supposed to be for comparing to a non-optional become the best overload
    candidates, but are ambiguous (and we don't even get as far as checking
    the constraints for satisfaction). I reported LWG 4072 for this.
    
    The paper does not change std::expected, but probably should have done.
    I'll submit an LWG issue about that and implement it separately.
    
    Also add [[nodiscard]] to all these comparison operators.
    
    libstdc++-v3/ChangeLog:
    
    	* include/bits/stl_pair.h (operator==): Add constraint.
    	* include/bits/version.def (constrained_equality): Define.
    	* include/bits/version.h: Regenerate.
    	* include/std/optional: Define feature test macro.
    	(__optional_rep_op_t): Use is_convertible_v instead of
    	is_convertible.
    	* include/std/tuple: Define feature test macro.
    	(operator==, __tuple_cmp, operator<=>): Reimplement C++20
    	comparisons using lambdas. Add constraints.
    	* include/std/utility: Define feature test macro.
    	* include/std/variant: Define feature test macro.
    	(_VARIANT_RELATION_FUNCTION_TEMPLATE): Add constraints.
    	(variant): Remove unnecessary friend declarations for comparison
    	operators.
    	* testsuite/20_util/optional/relops/constrained.cc: New test.
    	* testsuite/20_util/pair/comparison_operators/constrained.cc:
    	New test.
    	* testsuite/20_util/tuple/comparison_operators/constrained.cc:
    	New test.
    	* testsuite/20_util/variant/relops/constrained.cc: New test.
    	* testsuite/20_util/tuple/comparison_operators/overloaded.cc:
    	Disable for C++20 and later.
    	* testsuite/20_util/tuple/comparison_operators/overloaded2.cc:
    	Remove dg-error line for target c++20.