std::owner_less_C++中文网

定义于头文件 <memory>

(1)

template< class T >
struct owner_less; /* undefined */

(C++11 起)
(C++17 前)

template< class T = void >
struct owner_less; /* undefined */

(C++17 起)

template< class T >
struct owner_less<std::shared_ptr<T>>;

(2) (C++11 起)

template< class T >
struct owner_less<std::weak_ptr<T>>;

(3) (C++11 起)

template<>
struct owner_less<void>;

(4) (C++17 起)

此函数对象提供基于拥有者(不同于基于值)的, std::weak_ptrstd::shared_ptr 两者的混合类型序。顺序满足二个智能指针比较相等,若且唯若它们均为空或共享所有权,即使由 get() 获得的裸指针值相异(例如因为它们指向同一对象中的不同子对象)。

在以 std::shared_ptrstd::weak_ptr 为关键建立关联容器,即

std::map<std::shared_ptr<T>, U, std::owner_less<std::shared_ptr<T>>>

std::map<std::weak_ptr<T>, U, std::owner_less<std::weak_ptr<T>>> 时,最好使用此类模板。

默认的 operator< 不为弱指针定义,并且可能错误地认为同一对象的二个共享指针不等价(见 shared_ptr::owner_before )。

特化

成员函数

用基于拥有者的语义比较其参数
(函数)

std::owner_less::operator()

所有模板特化的成员

bool operator()( const std::shared_ptr<T>& lhs,
                 const std::weak_ptr<T>& rhs ) const noexcept;

(C++11 起)

bool operator()( const std::weak_ptr<T>& lhs,
                 const std::shared_ptr<T>& rhs ) const noexcept;

(C++11 起)

仅为 owner_less<shared_ptr<T>> 模板特化的成员

bool operator()( const std::shared_ptr<T>& lhs,
                 const std::shared_ptr<T>& rhs ) const noexcept;

(C++11 起)

仅为 owner_less<weak_ptr<T>> 模板特化的成员

bool operator()( const std::weak_ptr<T>& lhs,
                 const std::weak_ptr<T>& rhs ) const noexcept;

(C++11 起)

用基于拥有者的语义比较 lhsrhs 。等效于调用 lhs.owner_before(rhs)

顺序是严格弱序关系。

lhsrhs 相等,当且仅当它们均为空或共享所有权。

参数

返回值

若按基于拥有者的顺序确定 lhs 小于 rhs ,则为 true

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

DR 应用于 出版时的行为 正确行为
LWG 2873 C++11 operator() 可以不声明为 noexcept 声明为 noexcept

参阅