Movatterモバイル変換


[0]ホーム

URL:



This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofWP status.

3431.<=> for containers should requirethree_way_comparable<T> instead of<=>

Section: 23.2.2.4[container.opt.reqmts]Status:WPSubmitter: Jonathan WakelyOpened: 2020-04-17Last modified: 2023-11-22

Priority:2

View all issues withWP status.

Discussion:

The precondition for<=> on containers is:

"Either<=> is defined for values of type (possiblyconst)T, or< is defined for values of type (possiblyconst)T and< is a total ordering relationship."

I don't think<=> is sufficient, becausesynth-three-way won't use<=> unlessthree_way_comparable<T> is satisfied, which requiresweakly-equality-comparable-with<T, T> as well as<=>.

So to use<=> I think the type also requires==, or more precisely, itmust satisfythree_way_comparable.

The problem becomes clearer with the following example:

#include <compare>#include <vector>struct X{  friend std::strong_ordering operator<=>(X, X) { return std::strong_ordering::equal; }};std::vector<X> v(1);std::strong_ordering c = v <=> v;

This doesn't compile, because despiteX meeting the preconditions for<=> in [tab:container.opt],synth-three-way will returnstd::weak_ordering.

Here is another example:

#include <compare>#include <vector>struct X{  friend bool operator<(X, X) { return true; } //The return value is intentional, see below  friend std::strong_ordering operator<=>(X, X) { return std::strong_ordering::equal; }};std::vector<X> v(1);std::weak_ordering c = v <=> v;

This meets the precondition because it defines<=>, but the result of<=> onvector<X> will be nonsense, becausesynth-three-way will useoperator< notoperator<=> and that defines a broken ordering.

So we're stating a precondition which implies "if you do this, you don't get garbage results" and then we give garbage results anyway.

The proposed resolution is one way to fix that, by tightening the precondition so that it matches whatsynth-three-way actually does.

[2020-04-25 Issue Prioritization]

Priority to 2 after reflector discussion.

Previous resolution [SUPERSEDED]:

This wording is relative toN4861.

  1. Modify 23.2.2[container.requirements.general], Table [tab:container.opt], as indicated:

    Table 75: Optional container operations [tab:container.opt]
    ExpressionReturn typeOperational
    semantics
    Assertion/note
    pre-/post-condition
    Complexity
    a <=> bsynth-three-way-result<value_type>lexicographical_compare_three_way(
    a.begin(), a.end(), b.begin(), b.end(),
    synth-three-way)
    Preconditions: Either<=> is defined for
    values of type (possiblyconst)

    Tsatisfiesthree_way_comparable,
    or< is defined for values of type
    (possiblyconst)T and
    < is a total ordering relationship.
    linear

[2022-04-24; Daniel rebases wording onN4910]

Previous resolution [SUPERSEDED]:

This wording is relative toN4910.

  1. Modify 23.2.2.4[container.opt.reqmts] as indicated:

    a <=> b

    -2-Result:synth-three-way-result<X::value_type>.

    -3-Preconditions: Either<=> is defined for values of type (possiblyconst)Tsatisfiesthree_way_comparable, or< is defined for values of type (possiblyconst)T and< is a total ordering relationship.

    -4-Returns:lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end(),synth-three-way)

    [Note 1: The algorithmlexicographical_compare_three_way is defined in Clause 27. —end note]

    -5-Complexity: Linear.

[2023-06-13; Varna]

The group liked the previously suggested wording but would prefer to say "models" instead of "satisfies"in preconditions.

[2023-06-14 Varna; Move to Ready]

[2023-11-11 Approved at November 2023 meeting in Kona. Status changed: Voting → WP.]

Proposed resolution:

This wording is relative toN4950.

  1. Modify 23.2.2.4[container.opt.reqmts] as indicated:

    a <=> b

    -2-Result:synth-three-way-result<X::value_type>.

    -3-Preconditions: Either<=> is defined for values of type (possiblyconst)Tmodelsthree_way_comparable, or< is defined for values of type (possiblyconst)T and< is a total ordering relationship.

    -4-Returns:lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end(),synth-three-way)

    [Note 1: The algorithmlexicographical_compare_three_way is defined in Clause 27. —end note]

    -5-Complexity: Linear.


[8]ページ先頭

©2009-2026 Movatter.jp