最終更新日時(UTC):
が更新

履歴 編集

function template
<memory>

std::operator<(C++11)

namespace std {
  // operator<=>により、以下の演算子が使用可能になる (C++20)
  template <class T, class U>
  bool operator<(const shared_ptr<T>& a,
                 const shared_ptr<U>& b) noexcept; // (1) C++11

  template <class T>
  bool operator<(const shared_ptr<T>& x,
                 nullptr_t) noexcept;              // (2) C++11

  template <class T>
  bool operator<(nullptr_t,
                 const shared_ptr<T>& x) noexcept; // (3) C++11
}

概要

shared_ptrにおいて、左辺が右辺より小さいかを判定する。

比較対象は、shared_ptrが指す値ではなく、shared_ptrが保持するポインタ値。これは「値ベース(value-based)な比較」と呼ばれる。「所有権ベース(ownership-based)な比較」は、owner_before()を参照。

戻り値

  • (1)
  • (2)
    • C++11 : std::less<T*>()(x.get(), nullptr)で比較した結果を返す。
    • C++17 : std::less<typename shared_ptr<T>::element_type*>()(x.get(), nullptr)で比較した結果を返す。
  • (3)
    • C++11 : std::less<T*>()(nullptr, x.get())で比較した結果を返す。
    • C++17 : std::less<typename shared_ptr<T>::element_type*>()(nullptr, x.get())で比較した結果を返す。

#include <iostream>
#include <memory>

int main()
{
  std::cout << std::boolalpha;

  std::shared_ptr<int> p1(new int(3));
  std::shared_ptr<int> p2(new int(3));

  bool r1 = p1 < p2;
  std::cout << r1 << std::endl;

  bool r2 = p1 < nullptr;
  std::cout << r2 << std::endl;

  bool r3 = nullptr < p1;
  std::cout << r3 << std::endl;
}

出力例

false
false
true

バージョン

言語

  • C++11

処理系

  • GCC: 4.3.6 (nullptrバージョン以外), 4.7.4
  • Clang: 3.0 (nullptrバージョン以外), 3.3
  • ICC: ?
  • Visual C++: 2008 (TR1), 2010, 2012, 2013
    • 2012まではnullptrバージョンがない。

参照