• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <memory>

    std::operator==

    namespace std {
      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
    
      // (2)により、以下のオーバーロードが使用可能になる (C++20)
      template <class T>
      bool operator==(nullptr_t,
                      const shared_ptr<T>& x) noexcept; // (3) C++11
    }
    

    概要

    等値比較。

    戻り値

    備考

    • この演算子により、以下の演算子が使用可能になる (C++20):
      • operator!=

    #include <iostream>
    #include <memory>
    
    int main()
    {
      std::shared_ptr<int> p1(new int(3));
      std::shared_ptr<int> p2 = p1;
      if (p1 == p2) {
        std::cout << "equal" << std::endl;
      }
    
      std::shared_ptr<int> p3;
      if (p3 == nullptr) {
        std::cout << "p3 is nullptr" << std::endl;
      }
    
      if (nullptr == p3) {
        std::cout << "p3 is nullptr" << std::endl;
      }
    }
    

    出力

    equal
    p3 is nullptr
    p3 is nullptr
    

    バージョン

    言語

    • C++11

    処理系

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

    参照