Export (0) Print
Expand All


Const cast to shared_ptr.

template <class Ty, class Other>
    shared_ptr<Ty> const_pointer_cast(const shared_ptr<Other>& sp);


The type controlled by the returned shared pointer.


The type controlled by the argument shared pointer.


The argument shared pointer.

The template function returns an empty shared_ptr object if const_cast<Ty*>(sp.get()) returns a null pointer; otherwise it returns a shared_ptr Class<Ty> object that owns the resource that is owned by sp. The expression const_cast<Ty*>(sp.get()) must be valid.


// std_tr1__memory__const_pointer_cast.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
int main() 
    std::shared_ptr<int> sp0(new int); 
    std::shared_ptr<const int> sp1 = 
        std::const_pointer_cast<const int>(sp0); 
    *sp0 = 3; 
    std::cout << "sp1 == " << *sp1 << std::endl; 
    return (0); 
sp1 == 3

Header: <memory>

Namespace: std

© 2014 Microsoft