Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::owner_less

      From cppreference.com
      <cpp‎ |memory
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
      Defined in header<memory>
      (1)
      template<class T>
      struct owner_less;/* undefined */
      (since C++11)
      (until C++17)
      template<class T=void>
      struct owner_less;/* undefined */
      (since C++17)
      template<class T>
      struct owner_less<std::shared_ptr<T>>;
      (2)(since C++11)
      template<class T>
      struct owner_less<std::weak_ptr<T>>;
      (3)(since C++11)
      template<>
      struct owner_less<void>;
      (4)(since C++17)

      This function object provides owner-based (as opposed to value-based) mixed-type ordering of bothstd::weak_ptr andstd::shared_ptr. The order is such that two smart pointers compare equivalent only if they are both empty or if they share ownership, even if the values of the raw pointers obtained byget() are different (e.g. because they point at different subobjects within the same object).

      1) Owner-based mixed-type ordering is not provided for types other thanstd::shared_ptr andstd::weak_ptr.
      2) The owner-based mixed-type ordering ofstd::shared_ptr.
      It is the preferred comparison predicate when building associative containers withstd::shared_ptr as keys, that is,std::map<std::shared_ptr<T>, U, std::owner_less<std::shared_ptr<T>>>.
      3) The owner-based mixed-type ordering ofstd::weak_ptr.
      It is the preferred comparison predicate when building associative containers withstd::weak_ptr as keys, that is,std::map<std::weak_ptr<T>, U, std::owner_less<std::weak_ptr<T>>>.
      4) Thevoid specialization deduces the parameter types from the arguments.

      The defaultoperator< is not defined for weak pointers, and may wrongly consider two shared pointers for the same object non-equivalent (seestd::shared_ptr::owner_before).

      Contents

      Specializations

      The standard library provides a specialization ofstd::owner_less whenT is not specified. In this case, the parameter types are deduced from the arguments (each of which must still be either astd::shared_ptr or astd::weak_ptr).

      function object providing mixed-type owner-based ordering of shared and weak pointers, regardless of the type of the pointee
      (class template specialization)[edit]
      (since C++17)


      Nested types

      Nested type Definition
      result_type(deprecated in C++17)(2,3)bool
      first_argument_type(deprecated in C++17)(2)std::shared_ptr<T>
      (3)std::weak_ptr<T>
      second_argument_type(deprecated in C++17)(2)std::shared_ptr<T>
      (3)std::weak_ptr<T>
      (until C++20)

      [edit]Member functions

      operator()
      compares its arguments using owner-based semantics
      (function)

      std::owner_less::operator()

      member only of specialization(2)
      bool operator()(conststd::shared_ptr<T>& lhs,
                       conststd::shared_ptr<T>& rhs)constnoexcept;
      (since C++11)
      member only of specialization(3)
      bool operator()(conststd::weak_ptr<T>& lhs,
                       conststd::weak_ptr<T>& rhs)constnoexcept;
      (since C++11)
      member of both template specializations
      bool operator()(conststd::shared_ptr<T>& lhs,
                       conststd::weak_ptr<T>& rhs)constnoexcept;
      (since C++11)
      bool operator()(conststd::weak_ptr<T>& lhs,
                       conststd::shared_ptr<T>& rhs)constnoexcept;
      (since C++11)

      Compareslhs andrhs using owner-based semantics. Effectively callslhs.owner_before(rhs).

      The ordering is strict weak ordering relation.

      lhs andrhs are equivalent only if they are both empty or share ownership.

      Parameters

      lhs, rhs - shared-ownership pointers to compare

      Return value

      true iflhs isless thanrhs as determined by the owner-based ordering,false otherwise.

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2873C++11operator() was not required to be noexceptrequired to be noexcept

      [edit]See also

      provides owner-based ordering of shared pointers
      (public member function ofstd::shared_ptr<T>)[edit]
      provides owner-based ordering of weak pointers
      (public member function ofstd::weak_ptr<T>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/owner_less&oldid=164269"

      [8]ページ先頭

      ©2009-2025 Movatter.jp