Programing

C ++에서 <=> 연산자는 무엇입니까?

lottogame 2020. 5. 10. 10:16
반응형

C ++에서 <=> 연산자는 무엇입니까?


C ++ 연산자 에 대해 배우려고하는 동안 cppreference.com 에서 이상한 비교 연산자를 발견했습니다 . * 는 다음과 같습니다.

여기에 이미지 설명을 입력하십시오

"글쎄, 이것이 C ++에서 일반적인 연산자라면, 그것들을 더 잘 배우는 것"이라고 생각했습니다. 그러나이 수수께끼를 설명하려는 모든 시도는 실패했습니다. 여기에서도 스택 오버플로에서 검색에 운이 없었습니다.

<=>C ++ 사이에 연결이 있습니까?

그리고이 연산자가 있다면 정확히 무엇을합니까?

* 한편 cppreference.com은 해당 페이지를 업데이트했으며 이제 <=>운영자 에 대한 정보가 포함되어 있습니다 .


이것을 3 방향 비교 연산자 라고합니다 .

P0515 논문 제안 에 따르면 :

새로운 3 방향 비교 연산자가 <=>있습니다. 발현은 a <=> b비교 객체를 반환 <0하는 경우 a < b, 비교 >0하는 경우를 a > b, 비교하고 ==0있는 경우 ab동일한 / 당량이다.

유형에 대한 모든 비교를 작성하려면 operator<=>적절한 카테고리 유형을 리턴하는 작성하십시오 .

  • 돌아 _ordering를 당신의 유형은 자연적으로 지원하는 경우 <, 우리는 효율적으로 생성 할 수 있습니다 <, >, <=, >=, ==, 그리고 !=; 그렇지 않으면 _equality를 반환 하면 효율적으로 ==! =를 생성 합니다.

  • 유형에 대해 a == b암시하는 경우 강한 반환 f(a) == f(b)(substitutability, 여기서 f는 nonprivate const 인터페이스를 사용하여 액세스 가능한 비교 상태 만 읽음), 그렇지 않으면 약한 값을 반환합니다.

cppreference는 말합니다 :

3 방향 비교 연산자 식의 형식은 다음과 같습니다.

lhs <=> rhs   (1)  

이 표현식은 다음과 같은 객체를 반환합니다.

  • <0경우 비교lhs < rhs
  • >0경우 비교lhs > rhs
  • 비교하고 ==0있는 경우 lhsrhs동일한 / 당량이다.

2017년 11월 11일 상기 ISO C ++위원회 채용 허브 서터 용의 제안 <=> "우주선"삼원 비교 연산자 에 추가 된 새로운 기능들 중 하나로서 20 ++ C . 일관성있는 비교 Sutter 라는 논문 에서 Maurer와 Brown은 새로운 디자인의 개념을 보여줍니다. 제안에 대한 개요는 다음 기사에서 발췌 한 내용입니다.

a <=> b 표현식 a <b경우 <0 을 비교 하고 a> b경우 > 0을 비교 하고 a와 b가 같거나 동등한 경우 == 0 을 비교하는 객체를 반환합니다 .

Common case: To write all comparisons for your type X with type Y, with memberwise semantics, just write:

auto X::operator<=>(const Y&) =default;

Advanced cases: To write all comparisons for your type X with type Y, just write operator<=> that takes a Y, can use =default to get memberwise semantics if desired, and returns the appropriate category type:

  • Return an _ordering if your type naturally supports <, and we’ll efficiently generate symmetric <, >, <=, >=, ==, and !=; otherwise return an _equality, and we’ll efficiently generate symmetric == and !=.
  • Return strong_ if for your type a == b implies f(a) == f(b) (substitutability, where f reads only comparison-salient state that is accessible using the public const members), otherwise return weak_.

Comparison Categories

Five comparison categories are defined as std:: types, each having the following predefined values:

+--------------------------------------------------------------------+
|                  |          Numeric  values          | Non-numeric |
|     Category     +-----------------------------------+             |
|                  | -1   | 0          | +1            |   values    |
+------------------+------+------------+---------------+-------------+
| strong_ordering  | less | equal      | greater       |             |
| weak_ordering    | less | equivalent | greater       |             |
| partial_ordering | less | equivalent | greater       | unordered   |
| strong_equality  |      | equal      | nonequal      |             |
| weak_equality    |      | equivalent | nonequivalent |             |
+------------------+------+------------+---------------+-------------+

Implicit conversions between these types are defined as follows:

  • strong_ordering with values {less, equal, greater} implicitly converts to:
    • weak_ordering with values {less, equivalent, greater}
    • partial_ordering with values {less, equivalent, greater}
    • strong_equality with values {unequal, equal, unequal}
    • weak_equality with values {nonequivalent, equivalent, nonequivalent}
  • weak_ordering with values {less, equivalent, greater} implicitly converts to:
    • partial_ordering with values {less, equivalent, greater}
    • weak_equality with values {nonequivalent, equivalent, nonequivalent}
  • partial_ordering with values {less, equivalent, greater, unordered} implicitly converts to:
    • weak_equality with values {nonequivalent, equivalent, nonequivalent, nonequivalent}
  • strong_equality with values {equal, unequal} implicitly converts to:
    • weak_equality with values {equivalent, nonequivalent}

Three-way comparison

The<=>token is introduced. The character sequence<=>tokenizes to<= >, in old source code. For example,X<&Y::operator<=>needs to add a space to retain its meaning.

The overloadable operator<=>is a three-way comparison function and has precedence higher than< and lower than<<. It returns a type that can be compared against literal0but other return types are allowed such as to support expression templates. All<=>operators defined in the language and in the standard library return one of the 5 aforementionedstd::comparison category types.

For language types, the following built-in<=>same-type comparisons are provided. All are constexpr, except where noted otherwise. These comparisons cannot be invoked heterogeneously using scalar promotions/conversions.

  • Forbool, integral, and pointer types,<=>returnsstrong_ordering.
  • For pointer types, the different cv-qualifications and derived-to-base conversions are allowed to invoke a homogeneous built-in<=>, and there are built-in heterogeneousoperator<=>(T*, nullptr_t). Only comparisons of pointers to the same object/allocation are constant expressions.
  • For fundamental floating point types,<=> returnspartial_ordering, and can be invoked heterogeneously by widening arguments to a larger floating point type.
  • For enumerations,<=> returns the same as the enumeration's underlying type's<=>.
  • Fornullptr_t,<=> returnsstrong_orderingand always yieldsequal.
  • For copyable arrays,T[N] <=> T[N]returns the same type asT's<=>and performs lexicographical elementwise comparison. There is no<=>for other arrays.
  • Forvoidthere is no<=>.

To better understand the inner workings of this operator, please read the original paper. This is just what I've found out using search engines.


This answer has become irrelevant since the referenced web page has changed

The web page you are referencing was broken. It was being edited a lot that day and different parts was not in sync. The status when I was looking at it was:

At the top of the page it lists the currently existing comparison operators (in C++14). There is no <=>there.

At the bottom of the page, they should have listed the same operators, but they goofed and added this future suggestion.

gcc에 대해 <=>아직 알지 -std=c++14못하며 결코 의지하지 않을 것 a <= > b입니다. 오류 메시지에 대한 설명입니다.

5 년 후에 같은 것을 시도하면 아마도 더 나은 오류 메시지를 보게 될 것입니다. <=> not part of C++14.

참고 URL : https://stackoverflow.com/questions/47466358/what-is-the-operator-in-c

반응형