Namespaces
Variants

operator==, !=, <, <=, >, >=, <=> (std::shared_ptr)

From cppreference.net
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* )
( 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)
헤더 파일에 정의됨 <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> 와 null 포인터를 비교합니다.

shared_ptr 의 비교 연산자는 단순히 포인터 값을 비교한다는 점에 유의하십시오; 가리키는 실제 객체는 비교되지 않습니다 . shared_ptr 에 대해 operator< 가 정의되어 있으면 shared_ptr std::map std::set 과 같은 연관 컨테이너의 키로 사용할 수 있습니다.

< , <= , > , >= , 그리고 != 연산자들은 각각 operator <=> operator == 로부터 합성됩니다 .

(C++20부터)

목차

매개변수

lhs - 비교할 왼쪽 shared_ptr
rhs - 비교할 오른쪽 shared_ptr

반환값

1) lhs. get ( ) == rhs. get ( )
2) ! ( lhs == rhs )
3) std:: less < V > ( ) ( lhs. get ( ) , rhs. get ( ) ) , 여기서 V는 복합 포인터 타입 으로서 std:: shared_ptr < T > :: element_type * std:: shared_ptr < U > :: element_type * 의 조합입니다.
4) rhs < lhs
5) ! ( rhs < lhs )
6) ! ( lhs < rhs )
7) std:: compare_three_way { } ( x. get ( ) , y. get ( ) )
8) ! lhs
9) ! rhs
10) ( bool ) lhs
11) ( bool ) rhs
12) std:: less < std:: shared_ptr < T > :: element_type * > ( ) ( lhs. get ( ) , nullptr )
13) std:: less < std:: shared_ptr < T > :: element_type * > ( ) ( nullptr, rhs. get ( ) )
14) nullptr < lhs
15) rhs < nullptr
16) ! ( nullptr < lhs )
17) ! ( rhs < nullptr )
18) ! ( lhs < nullptr )
19) ! ( nullptr < rhs )
20) std:: compare_three_way { } ( x. get ( ) , static_cast < std:: shared_ptr < T > :: element_type * > ( nullptr ) )

참고 사항

모든 경우에, 비교되는 것은 관리되는 포인터( use_count 가 0이 될 때 deleter에 전달되는 포인터)가 아니라 저장된 포인터( get() 에 의해 반환되는 포인터)입니다. 두 포인터는 aliasing constructor를 사용하여 생성된 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++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3427 C++20 operator<=>(shared_ptr, nullptr_t) 가 잘못된 형태였음 정의 수정됨

참고 항목

저장된 포인터를 반환합니다
(public member function)