operator==, !=, <, <=, >, >=, <=> (std::shared_ptr)
|
헤더 파일에 정의됨
<memory>
|
||
|
두
shared_ptr
객체를 비교합니다.
|
||
|
template
<
class
T,
class
U
>
bool
operator
==
(
const
std::
shared_ptr
<
T
>
&
lhs,
|
(1) | (C++11 이후) |
|
template
<
class
T,
class
U
>
bool
operator
!
=
(
const
std::
shared_ptr
<
T
>
&
lhs,
|
(2) |
(C++11부터)
(C++20까지) |
|
template
<
class
T,
class
U
>
bool
operator
<
(
const
std::
shared_ptr
<
T
>
&
lhs,
|
(3) |
(C++11부터)
(C++20까지) |
|
template
<
class
T,
class
U
>
bool
operator
>
(
const
std::
shared_ptr
<
T
>
&
lhs,
|
(4) |
(C++11 이후)
(C++20 이전) |
|
template
<
class
T,
class
U
>
bool
operator
<=
(
const
std::
shared_ptr
<
T
>
&
lhs,
|
(5) |
(C++11 이후)
(C++20 이전) |
|
template
<
class
T,
class
U
>
bool
operator
>=
(
const
std::
shared_ptr
<
T
>
&
lhs,
|
(6) |
(C++11부터)
(C++20 이전까지) |
|
template
<
class
T,
class
U
>
std::
strong_ordering
operator
<=>
(
const
std::
shared_ptr
<
T
>
&
lhs,
|
(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,
|
(20) | (C++20 이후) |
두 개의
shared_ptr<T>
객체를 비교하거나
shared_ptr<T>
와 null 포인터를 비교합니다.
shared_ptr
의 비교 연산자는 단순히 포인터 값을 비교한다는 점에 유의하십시오; 가리키는 실제 객체는
비교되지 않습니다
.
shared_ptr
에 대해
operator<
가 정의되어 있으면
shared_ptr
을
std::map
과
std::set
과 같은 연관 컨테이너의 키로 사용할 수 있습니다.
|
|
(C++20부터) |
목차 |
매개변수
| lhs | - |
비교할 왼쪽
shared_ptr
|
| rhs | - |
비교할 오른쪽
shared_ptr
|
반환값
참고 사항
모든 경우에, 비교되는 것은 관리되는 포인터( 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) |