std::owner_less

定义于头文件 <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_lessT 未指定时的特化。此情况下,形参类型从实参类型推导出(每个实参必须是一个 std::shared_ptrstd::weak_ptr )。

为共享指针和弱指针提供混合类型的、基于拥有者的顺序的函数对象,无关乎被指向的类型
(类模板特化)
(C++17 起)

成员类型

成员类型 定义
result_type(C++17 中弃用) 2-3) bool
first_argument_type(C++17 中弃用) 2) std::shared_ptr<T>
3) std::weak_ptr<T>
second_argument_type(C++17 中弃用) 2) std::shared_ptr<T>
3) std::weak_ptr<T>

成员函数

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

std::owner_less::operator()

bool operator()( const std::shared_ptr<T>& lhs,
                 const std::shared_ptr<T>& rhs ) const;
(C++11 起)
(仅为 owner_less<shared_ptr<T>> 模板特化的成员)
bool operator()( const std::shared_ptr<T>& lhs,
                 const std::weak_ptr<T>& rhs ) const;
(C++11 起)
bool operator()( const std::weak_ptr<T>& lhs,
                 const std::shared_ptr<T>& rhs ) const;
(C++11 起)
bool operator()( const std::weak_ptr<T>& lhs,
                 const std::weak_ptr<T>& rhs ) const;
(C++11 起)
(仅为 owner_less<weak_ptr<T>> 模板特化的成员)

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

顺序是严格弱序关系。

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

参数

lhs, rhs - 要比较的共享所有权指针

返回值

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

异常

(无) (C++17 前)
noexcept 规定:  
noexcept
  
(C++17 起)

参阅

提供基于拥有者的共享指针排序
(std::shared_ptr 的公开成员函数)
提供弱指针的基于拥有者顺序
(std::weak_ptr 的公开成员函数)

版本历史

  • (当前 | 先前 2017年9月9日 (六) 08:17Fruderica讨论 | 贡献 . . (3,805字节) (+2). . (撤销)
  • 当前 | 先前 2017年6月8日 (四) 07:46Fruderica讨论 | 贡献. . (3,803字节) (+875). . (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:05P12bot讨论 | 贡献 . . (2,928字节) (-173). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:48P12bot讨论 | 贡献 . . (3,101字节) (+233). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:39P12讨论 | 贡献 . . (2,868字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (2,868字节) (-48). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:09P12讨论 | 贡献 . . (2,916字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (2,916字节) (+2,916). . (Translated from the English version using Google Translate)