I seem to be missing something here. I moved from boost::shared_ptr
to std::shared_ptr
. shared_ptr
was part of TR1 back in the mid-2000s, and it should be available everywhere in 2012.
Trying to use shared_ptr
under Apple gets me a slew of undefined references:
SecureArray.h:26:12: error: no member named 'shared_ptr' in
namespace 'std'
using std::shared_ptr;
~~~~~^
SecureArray.h:27:12: error: no member named 'tr1' in namespace
'std'
using std::tr1::shared_ptr;
~~~~~^
SecureArray.h:487:5: error: unknown type name 'shared_ptr'
shared_ptr<SecureVector> m_vector;
A typical compiler command is as follows (both GCC and Clang fail):
clang++ -g2 -ggdb -O0 -fcatch-undefined-cxx0x-behavior
-DSAFEINT_DISALLOW_UNSIGNED_NEGATION=1 -pipe -std=c++0x -Wall -Wextra
-Wno-unused-parameter -Wno-tautological-compare
-I. -I./esapi -I./deps -I/usr/local/include -I/usr/include -fpic
-c src/DummyConfiguration.cpp -o src/DummyConfiguration.o
I'm trying to include it as follows (I believe I need to tweak this, but I don't recall the C++ syntax to say "look here, or look there"):
#include <memory>
using std::shared_ptr;
using std::tr1::shared_ptr;
Apple's man pages are not turning up anything:
$ man shared_ptr
No manual entry for shared_ptr
$ man -k shared_ptr
shared_ptr: nothing appropriate
I have Mac OS X 10.8 (fully patched), Xcode (fully patched), and Command Line Tools installed.
So how does one use a std::shared_ptr on Apple platforms?
So no, you shouldn't. The purpose of shared_ptr is to manage an object that no one "person" has the right or responsibility to delete, because there could be others sharing ownership. So you shouldn't ever want to, either.
this function is deprecated as of C++17 because use_count is only an approximation in multi-threaded environment.
std::shared_ptr::getReturns the stored pointer. The stored pointer points to the object the shared_ptr object dereferences to, which is generally the same as its owned pointer.
The smart pointer has an internal counter which is decreased each time that a std::shared_ptr , pointing to the same resource, goes out of scope – this technique is called reference counting. When the last shared pointer is destroyed, the counter goes to zero, and the memory is deallocated.
#include <tr1/memory>
will work with either compiler using libstdc++. Alternately, with Clang:
#include <memory>
using std::shared_ptr;
and compile with c++ -std=c++11 -stdlib=libc++ ...
. I have no idea why Clang is using libstdc++ by default; presumably it's for GCC compatibility.
You can't find the man pages because libstdc++ doesn't have man pages. Helpful, isn't it. There's HTML documentation in the source distribution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With