Skip to content

Files

101 lines (81 loc) · 2.85 KB

op_compare_3way.md

File metadata and controls

101 lines (81 loc) · 2.85 KB

operator<=>

  • memory[meta header]
  • std[meta namespace]
  • function template[meta id-type]
  • cpp20[meta cpp]
namespace std {
  template <class T1, class D1, class T2, class D2>
    requires three_way_comparable_with<
               typename unique_ptr<T1, D1>::pointer,
               typename unique_ptr<T2, D2>::pointer>
  compare_three_way_result_t<
    typename unique_ptr<T1, D1>::pointer,
    typename unique_ptr<T2, D2>::pointer>
  operator<=>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); // (1) C++20

  template <class T, class D>
    requires three_way_comparable_with<typename unique_ptr<T, D>::pointer>
  compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
  operator<=>(const unique_ptr<T, D>& x, nullptr_t);                     // (2) C++20

  template <class T, class D>
    requires three_way_comparable_with<typename unique_ptr<T, D>::pointer>
  constexpr compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
  operator<=>(const unique_ptr<T, D>& x, nullptr_t);                     // (2) C++23
}
  • nullptr_t[link /reference/cstddef/nullptr_t.md]

概要

unique_ptrオブジェクトの三方比較を行う。

テンプレートパラメータ制約

  • (1) : 型unique_ptr<T1, D1>::pointerと型unique_ptr<T2, D2>::pointerが三方比較可能であること
  • (2) : 型unique_ptr<T, D>::pointer同士が三方比較可能であること

戻り値

  • (1) :

    return compare_three_way()(x.get(), y.get());
    • compare_three_way[link /reference/compare/compare_three_way.md]
    • get()[link get.md]
  • (2) :

    return compare_three_way()(x.get(), static_cast<typename unique_ptr<T, D>::pointer>(nullptr));
    • compare_three_way[link /reference/compare/compare_three_way.md]
    • get()[link get.md]

#include <iostream>
#include <memory>

int main()
{
  std::unique_ptr<int> p1(new int(3));
  if ((p1 <=> p1) == 0) {
    std::cout << "equal" << std::endl;
  }

  std::unique_ptr<int> p2;
  if ((p2 <=> nullptr) == 0) {
    std::cout << "p2 is nullptr" << std::endl;
  }

  if ((nullptr <=> p2) == 0) {
    std::cout << "p2 is nullptr" << std::endl;
  }
}

出力

equal
p2 is nullptr
p2 is nullptr

バージョン

言語

  • C++20

処理系

参照