Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 1.91 KB

op_compare_3way.md

File metadata and controls

72 lines (53 loc) · 1.91 KB

operator<=>

  • memory[meta header]
  • std[meta namespace]
  • indirect[meta class]
  • function[meta id-type]
  • cpp26[meta cpp]
template <class U, class AA>
friend constexpr auto operator<=>(
  const indirect& lhs, const indirect<U, AA>& rhs)
    -> synth-three-way-result<T, U>; // (1)

template <class U>
friend constexpr auto operator<=>(
  const indirect& lhs, const U& rhs)
    -> synth-three-way-result<T, U>; // (2)

概要

  • (1) : 2つのindirectオブジェクトが所有する値を三方比較する。
  • (2) : indirectオブジェクトが所有する値と、別の値rhsを三方比較する。

いずれもHidden friendsとして定義される。

戻り値

  • (1) : lhsとrhsのいずれかが無効値状態であれば!lhs.valueless_after_move() <=> !rhs.valueless_after_move()、そうでなければsynth-three-way(*lhs, *rhs)。
  • (2) : lhsが無効値状態であればstd::strong_ordering::less、そうでなければsynth-three-way(*lhs, rhs)。

備考

この演算子により、operator< / operator<= / operator> / operator>=が使用可能になる。

例

#include <cassert>
#include <memory>

int main()
{
  std::indirect<int> a{1};
  std::indirect<int> b{2};
  assert(a < b);          // (1) indirect同士の比較
  assert((a <=> b) < 0);
  assert(a < 2);          // (2) 値との比較
}
  • std::indirect[link ../indirect.md]

出力

バージョン

言語

  • C++26

処理系

  • Clang: 22 [mark noimpl]
  • GCC: 16.1 [mark verified]
  • Visual C++: 2026 Update 2 [mark noimpl]

関連項目

参照