Share via


operator< <memory>

shared_ptr and weak_ptr less than comparison.

template<class Ty1, class Ty2>
    bool operator<(const shared_ptr<Ty1>& left, const shared_ptr<Ty2>& right);
template<class Ty1, class Ty2>
    bool operator<(const weak_ptr<Ty1>& left, const weak_ptr<Ty2>& right);

Parameters

  • Ty1
    The type controlled by the left shared pointer.

  • Ty2
    The type controlled by the right shared pointer.

  • left
    The left shared pointer.

  • right
    The right shared pointer.

Remarks

The template functions impose a strict weak ordering on objects of their respective types. The actual ordering is implementation-specific, except that !(left < right) && !(right < left) is true only when left and right own or point to the same resource. The ordering imposed by these functions enables the use of shared_ptr Class and weak_ptr Class objects as keys in associative containers.

Example

 

// std_tr1__memory__operator_lt.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
int main() 
    { 
    std::shared_ptr<int> sp0(new int(0)); 
    std::shared_ptr<int> sp1(new int(0)); 
 
    std::cout << "sp0 < sp0 == " << std::boolalpha 
        << (sp0 < sp0) << std::endl; 
    std::cout << "sp0 < sp1 == " << std::boolalpha 
        << (sp0 < sp1) << std::endl; 
    std::cout << "sp1 < sp0 == " << std::boolalpha 
        << (sp1 < sp0) << std::endl; 
    std::cout << std::endl; 
 
    std::weak_ptr<int> wp0(sp0); 
    std::weak_ptr<int> wp1(sp1); 
 
    std::cout << "wp0 < wp0 == " << std::boolalpha 
        << (wp0 < wp0) << std::endl; 
    std::cout << "wp0 < wp1 == " << std::boolalpha 
        << (wp0 < wp1) << std::endl; 
    std::cout << "wp1 < wp0 == " << std::boolalpha 
        << (wp1 < wp0) << std::endl; 
 
    return (0); 
    } 
 
sp0 < sp0 == false
sp0 < sp1 == true
sp1 < sp0 == false

wp0 < wp0 == false
wp0 < wp1 == true
wp1 < wp0 == false

Requirements

Header: <memory>

Namespace: std

See Also

Reference

<memory> (TR1)

shared_ptr Class

weak_ptr Class

operator== <memory>

operator!= <memory>