memory_owner_less
memory_owner_less (std::owner_less)
<memory> 헤더에 정의되어 있어요. std::shared_ptr와 std::weak_ptr에 대해 소유자 기반(값 기반이 아닌) 혼합 타입 순서 비교를 제공하는 함수 객체예요. C++11부터 도입됐어요.
출처: cppreference
본문
template< class T >
struct owner_less; /* undefined */ // (1) since C++11, until C++17
template< class T = void >
struct owner_less; /* undefined */ // (1) since C++17
template< class T >
struct owner_less<std::shared_ptr<T>>; // (2) since C++11
template< class T >
struct owner_less<std::weak_ptr<T>>; // (3) since C++11
template<>
struct owner_less<void>; // (4) since C++17
이 함수 객체는 std::shared_ptr와 std::weak_ptr에 대해 소유자 기반(값 기반이 아닌) 혼합 타입 순서 비교를 제공해요. 즉 get()으로 얻은 값이 아니라, 소유권(제어 블록)을 기준으로 비교해요. 두 스마트 포인터가 소유권을 공유하면 순서상 동등하게 비교돼요.
std::shared_ptr<int> a = std::make_shared<int>(1);
std::shared_ptr<int> b = a;
std::weak_ptr<int> w = a;
std::owner_less<std::shared_ptr<int>> cmp;
// (a, w), (w, a) 비교가 정의됨
// 소유권 기반 정렬 컨테이너
std::set<std::weak_ptr<int>, std::owner_less<std::weak_ptr<int>>> s;
owner_less<void>는 C++17에서 추가되어 혼합 타입(shared_ptr와 weak_ptr 섞임) 비교를 지원해요. 값 기반 비교인 기본 operator<와 달리 소유권 기반으로 동작하므로, 제어 블록이 같은 두 포인터는 항상 동등하게 정렬돼요.