operator==, !=, <, <=, >, >=, <=> (std::shared_ptr)
| Определено в заголовочном файле <memory>
|
||
| Сравнивает два объекта shared_ptr |
||
template < class T, class U > bool operator==( const std::shared_ptr<T>& lhs, const std::shared_ptr<U>& rhs ) noexcept; |
(1) | (начиная с C++11) |
template< class T, class U > bool operator!=( const std::shared_ptr<T>& lhs, const std::shared_ptr<U>& rhs ) noexcept; |
(2) | (начиная с C++11) (до C++20) |
template< class T, class U > bool operator<( const std::shared_ptr<T>& lhs, const std::shared_ptr<U>& rhs ) noexcept; |
(3) | (начиная с C++11) (до C++20) |
template< class T, class U > bool operator>( const std::shared_ptr<T>& lhs, const std::shared_ptr<U>& rhs ) noexcept; |
(4) | (начиная с C++11) (до C++20) |
template< class T, class U > bool operator<=( const std::shared_ptr<T>& lhs, const std::shared_ptr<U>& rhs ) noexcept; |
(5) | (начиная с C++11) (до C++20) |
template< class T, class U > bool operator>=( const std::shared_ptr<T>& lhs, const std::shared_ptr<U>& rhs ) noexcept; |
(6) | (начиная с C++11) (до C++20) |
template< class T, class U > std::strong_ordering operator<=>( const std::shared_ptr<T>& lhs, const std::shared_ptr<U>& rhs ) noexcept; |
(7) | (начиная с C++20) |
| Сравнивает shared_ptr с нулевым указателем |
||
template< class T > bool operator==( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; |
(8) | (начиная с C++11) |
template< class T > bool operator==( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; |
(9) | (начиная с C++11) (до C++20) |
template< class T > bool operator!=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; |
(10) | (начиная с C++11) (до C++20) |
template< class T > bool operator!=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; |
(11) | (начиная с C++11) (до C++20) |
template< class T > bool operator<( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; |
(12) | (начиная с C++11) (до C++20) |
template< class T > bool operator<( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; |
(13) | (начиная с C++11) (до C++20) |
template< class T > bool operator>( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; |
(14) | (начиная с C++11) (до C++20) |
template< class T > bool operator>( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; |
(15) | (начиная с C++11) (до C++20) |
template< class T > bool operator<=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; |
(16) | (начиная с C++11) (до C++20) |
template< class T > bool operator<=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; |
(17) | (начиная с C++11) (до C++20) |
template< class T > bool operator>=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; |
(18) | (начиная с C++11) (до C++20) |
template< class T > bool operator>=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; |
(19) | (начиная с C++11) (до C++20) |
template< class T > std::strong_ordering operator<=>( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; |
(20) | (начиная с C++20) |
Сравнивает два объекта shared_ptr<T> или сравнивает shared_ptr<T> с нулевым указателем.
Обратите внимание: операции сравнения для shared_ptr просто сравнивают значения указателей; фактические объекты, на которые они указывают, не сравниваются. Наличие operator<, определённого для shared_ptr, позволяет использовать shared_ptr в качестве ключей в ассоциативных контейнерах, таких как std::map и std::set.
|
Операторы |
(начиная с C++20) |
Параметры
| lhs | — | левый shared_ptr для сравнения
|
| rhs | — | правый shared_ptr для сравнения
|
Возвращаемое значение
lhs.get() == rhs.get()!(lhs == rhs)std::less<V>()(lhs.get(), rhs.get()), где V это тип составного указателя std::shared_ptr<T>::element_type* и std::shared_ptr<U>::element_type*rhs < lhs!(rhs < lhs)!(lhs < rhs)std::compare_three_way{}(x.get(), y.get())!lhs!rhs(bool)lhs(bool)rhs std::less<std::shared_ptr<T>::element_type*>()(lhs.get(), nullptr)std::less<std::shared_ptr<T>::element_type*>()(nullptr, rhs.get())nullptr < lhsrhs < nullptr!(nullptr < lhs)!(rhs < nullptr)!(lhs < nullptr)!(nullptr < rhs)std::compare_three_way{}(x.get(), static_cast<std::shared_ptr<T>::element_type*>(nullptr))Примечание
Во всех случаях сравнивается сохранённый указатель (тот, который возвращается функцией get()), а не управляемый указатель (тот, который передаётся средству удаления, когда use_count переходит в нуль). Два указателя могут различаться в std::shared_ptr, созданном с помощью конструктора псевдонимов.
Пример
#include <iostream>
#include <memory>
int main()
{
std::shared_ptr<int> p1(new int(42));
std::shared_ptr<int> p2(new int(42));
std::cout << std::boolalpha
<< "(p1 == p1) : " << (p1 == p1) << '\n'
<< "(p1 <=> p1) == 0 : " << ((p1 <=> p1) == 0) << '\n' // Начиная с C++20
// p1 и p2 указывают на разные ячейки памяти, поэтому p1 != p2
<< "(p1 == p2) : " << (p1 == p2) << '\n'
<< "(p1 < p2) : " << (p1 < p2) << '\n'
<< "(p1 <=> p2) < 0 : " << ((p1 <=> p2) < 0) << '\n' // Начиная с C++20
<< "(p1 <=> p2) == 0 : " << ((p1 <=> p2) == 0) << '\n'; // Начиная с C++20
}
Возможный вывод:
(p1 == p1) : true
(p1 <=> p1) == 0 : true
(p1 == p2) : false
(p1 < p2) : true
(p1 <=> p2) < 0 : true
(p1 <=> p2) == 0 : false
Отчёты о дефектах
Следующие изменения поведения были применены с обратной силой к ранее опубликованным стандартам C++:
| Номер | Применён | Поведение в стандарте | Корректное поведение |
|---|---|---|---|
| LWG 3427 | C++20 | operator<=>(shared_ptr, nullptr_t) некорректен
|
определение зафиксировано |
Смотрите также
| возвращает хранимый указатель (public функция-элемент) |