• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <memory>

    std::operator<

    namespace std {
      template <class T1, class D1, class T2, class D2>
      bool operator<(const unique_ptr<T1, D1>& a, const unique_ptr<T2, D2>& b); // (1) C++11
    
      template <class T, class D>
      bool operator<(const unique_ptr<T, D>& x, nullptr_t);                     // (2) C++11
      template <class T, class D>
      constexpr bool operator<(const unique_ptr<T, D>& x, nullptr_t);           // (2) C++23
    
      template <class T, class D>
      bool operator<(nullptr_t, const unique_ptr<T, D>& x);                     // (3) C++11
      template <class T, class D>
      constexpr bool operator<(nullptr_t, const unique_ptr<T, D>& x);           // (3) C++23
    }
    

    概要

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

    比較対象は、unique_ptrが指す値ではなく、unique_ptrが保持するポインタ値。

    戻り値

    • (1) : std::common_type<unique_ptr<T1, D1>::pointer, unique_ptr<T2, D2>::pointer>::typeを、abが持つポインタの共通の型CTとし、std::less<CT>(a.get(), b.get())で比較した結果を返す。

    • (2) : std::less<unique_ptr<T, D>::pointer>()(x.get(), nullptr)で比較した結果を返す。

    • (3) : std::less<unique_ptr<T, D>::pointer>()(nullptr, x.get())で比較した結果を返す。

    #include <iostream>
    #include <memory>
    
    int main()
    {
      std::cout << std::boolalpha;
    
      std::unique_ptr<int> p1(new int(3));
      std::unique_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;
    }
    

    出力例

    true
    false
    true
    

    バージョン

    言語

    • C++11

    処理系

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

    参照