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

Does C++11 unique_ptr and shared_ptr able to convert to each other’s type?

std::unique_ptr is the C++11 way to express exclusive ownership, but one of its most attractive features is that it easily and efficiently converts to a std::shared_ptr. This is a key part of why std::unique_ptr is so well suited as a factory function return type. Factory functions can’t know whether callers will want to use exclusive … Read more

Does std::bind work with move-only types in general, and std::unique_ptr in particular?

std::bind works fine with move-only types. However it creates a move-only functor in the process. std::function requires a copy constructible functor. It sounds like boost::asio does too. When you call the move-only bind functor, it will pass its bound arguments as lvalues to the target operator(). So if one of your bound arguments is move-only, … Read more

Why is `make_unique` disallowed?

Quoting from the original proposal: T[N] As of N3485, unique_ptr doesn’t provide a partial specialization for T[N]. However, users will be strongly tempted to write make_unique<T[N]>(). This is a no-win scenario. Returning unique_ptr<T[N]> would select the primary template for single objects, which is bizarre. Returning unique_ptr<T[]> would be an exception to the otherwise ironclad rule … Read more