Raw pointer lookup for sets of unique_ptrs

In C++14, std::set<Key>::find is a template function if Compare::is_transparent exists. The type you pass in does not need to be Key, just equivalent under your comparator. So write a comparator: template<class T> struct pointer_comp { typedef std::true_type is_transparent; // helper does some magic in order to reduce the number of // pairs of types we … Read more

shared_ptr is to weak_ptr as unique_ptr is to… what?

The “notify” behavior of shared_ptr requires reference counting the reference count control block. shared_ptr‘s reference count control block(s) use separate reference counts for this. weak_ptr instances maintain references to this block, and weak_ptrs themselves prevent the reference count control block from being deleteed. The pointed-to object has its destructor called when the strong count goes … Read more

Is auto_ptr deprecated?

UPDATE: This answer was written in 2010 and as anticipated std::auto_ptr has been deprecated. The advice is entirely valid. In C++0x std::auto_ptr will be deprecated in favor of std::unique_ptr. The choice of smart pointer will depend on your use case and your requirements, with std::unique_ptr with move semantics for single ownership that can be used … Read more

error::make_unique is not a member of ‘std’

make_unique is an upcoming C++14 feature and thus might not be available on your compiler, even if it is C++11 compliant. You can however easily roll your own implementation: template<typename T, typename… Args> std::unique_ptr<T> make_unique(Args&&… args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)…)); } (FYI, here is the final version of make_unique that was voted into C++14. This … Read more

How can I pass std::unique_ptr into a function

There’s basically two options here: Pass the smart pointer by reference void MyFunc(unique_ptr<A> & arg) { cout << arg->GetVal() << endl; } int main(int argc, char* argv[]) { unique_ptr<A> ptr = unique_ptr<A>(new A(1234)); MyFunc(ptr); } Move the smart pointer into the function argument Note that in this case, the assertion will hold! void MyFunc(unique_ptr<A> arg) … Read more

How to make std::make_unique a friend of my class

make_unique perfect forwards the arguments you pass to it; in your example you’re passing an lvalue (x) to the function, so it’ll deduce the argument type as int&. Your friend function declaration needs to be friend std::unique_ptr<A> std::make_unique<A>(T&); Similarly, if you were to move(x) within CreateA, the friend declaration would need to be friend std::unique_ptr<A> … Read more

Calling initializer_list constructor via make_unique/make_shared

std::make_unique is a function template which deduces the argument types which are passed to the object constructor. Unfortunately, braced lists are not deducible (with an exception for auto declarations), and so you cannot instantiate the function template when that missing parameter type. You can either not use std::make_unique, but please don’t go that route – … Read more

std::auto_ptr to std::unique_ptr

You cannot do a global find/replace because you can copy an auto_ptr (with known consequences), but a unique_ptr can only be moved. Anything that looks like std::auto_ptr<int> p(new int); std::auto_ptr<int> p2 = p; will have to become at least like this std::unique_ptr<int> p(new int); std::unique_ptr<int> p2 = std::move(p); As for other differences, unique_ptr can handle … Read more