Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple and shared_ptr

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?

like image 241
jww Avatar asked Nov 18 '12 23:11

jww


People also ask

Do I need to delete a shared_ptr?

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.

Why is shared_ptr unique deprecated?

this function is deprecated as of C++17 because use_count is only an approximation in multi-threaded environment.

What does shared_ptr get () do?

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.

What happens when shared_ptr goes out of scope?

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.


1 Answers

#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.

like image 171
Hugh Avatar answered Oct 05 '22 22:10

Hugh