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

unique_ptr boost equivalent?

It’s not possible to create something like unique_ptr without C++0x (where it’s part of the standard library, and so Boost doesn’t need to provide it). Specifically without rvalue references, which are a feature in C++0x, a robust implementation of unique_ptr is impossible, with or without Boost. In C++03, there are a few possible alternatives, although … 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

C++ inserting unique_ptr in map

As a first remark, I wouldn’t call it ObjectArray if it is a map and not an array. Anyway, you can insert objects this way: ObjectArray myMap; myMap.insert(std::make_pair(0, std::unique_ptr<Class1>(new Class1()))); Or this way: ObjectArray myMap; myMap[0] = std::unique_ptr<Class1>(new Class1()); The difference between the two forms is that the former will fail if the key 0 … 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

Exception safety and make_unique

Not only when you have multiple allocations, but whenever you can throw at different places. Consider this: f(make_unique<T>(), function_that_can_throw()); Versus: f(unique_ptr<T>(new T), function_that_can_throw()); In the second case, the compiler is allowed to call (in order): new T function_that_can_throw() unique_ptr<T>(…) Obviously if function_that_can_throw actually throws then you leak. make_unique prevents this case. And of course, a … 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