Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

weak_ptr::swap

Swaps two weak_ptr objects.

void swap(weak_ptr& wp);

wp

The weak pointer to swap with.

The member function leaves the resource originally pointed to by *this subsequently pointed to by wp, and the resource originally pointed to by wp subsequently pointed to by *this. The function does not change the reference counts for the two resources and it does not throw any exceptions.

 

// std_tr1__memory__weak_ptr_swap.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
struct deleter 
    { 
    void operator()(int *p) 
        { 
        delete p; 
        } 
    }; 
 
int main() 
    { 
    std::shared_ptr<int> sp1(new int(5)); 
    std::shared_ptr<int> sp2(new int(10)); 
    std::cout << "*sp1 == " << *sp1 << std::endl; 
 
    sp1.swap(sp2); 
    std::cout << "*sp1 == " << *sp1 << std::endl; 
 
    swap(sp1, sp2); 
    std::cout << "*sp1 == " << *sp1 << std::endl; 
    std::cout << std::endl; 
 
    std::weak_ptr<int> wp1(sp1); 
    std::weak_ptr<int> wp2(sp2); 
    std::cout << "*wp1 == " << *wp1.lock() << std::endl; 
 
    wp1.swap(wp2); 
    std::cout << "*wp1 == " << *wp1.lock() << std::endl; 
 
    swap(wp1, wp2); 
    std::cout << "*wp1 == " << *wp1.lock() << std::endl; 
 
    return (0); 
    } 
 
*sp1 == 5
*sp1 == 10
*sp1 == 5

*wp1 == 5
*wp1 == 10
*wp1 == 5

Header: <memory>

Namespace: std

Show:
© 2014 Microsoft