Doodler Doodler - 1 year ago 97
C++ Question

How can a default-constructed object of a non-copyable class be emplaced into a boost::container::map?

Consider the folllowing C++03 code (I have to make the code compatible with pre-C++11 compilers):

// This class belongs to a third-party SDK and cannot be touched
class A {
explicit A();
explicit A(bool b);
// Non-copyable
A(const A&);
const A& operator= (const A&);

boost::container::map<int, A> myMap;

Using a Boost map here because it allows emplacing even in C++03. The problem is that I can perflectly emplace-construct into the map if using the one-argument constructor, but I don't know how to default-construct the object, as this code illustrates:

myMap.emplace(1, true); // Works
myMap.emplace(1); // Fails

The second call fails because it's taken as a call to the emplace(std::pair...) overload so it seems there is no way to "default-emplace".

Is there any way to achieve what I want?

Answer Source

Out of interest I played with this

As an important datapoint, I knew that std::map (in C++11) supports piecewise construction of it's value pairs:

std::map<int, A> stdMap;

would therefore invoke the contructor you're after. However, somehow, the same doesn't immediately work for Boost's map. Hmmm.

However, this piqued my interest: boost uses std::pair<const K, V> as the value type?!

boost::container::map<int, A>::value_type p { 

works no problem. And I can also verify that this typedef is in fact the type stored:

static_assert(std::is_same<decltype(p), std::remove_reference<decltype(*myMap.begin())>::type>::value, "Nonstandard pair");

So, it's beginning to look like a bug in the forwarding via the interal tree implementation when it uses the allocator::construct call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download