3

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 {
public:
    explicit A();
    explicit A(bool b);
private:
    // 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?

3 Answers 3

4

By making A moveable, but not copyable, and then moving the object to the emplacement function.

In C++03, you can use Boost.Move to make A moveable. Example:

#include <boost/container/map.hpp>
#include <boost/move/move.hpp>
#include <cassert>

class A { 
    BOOST_MOVABLE_BUT_NOT_COPYABLE(A)
public:
    explicit A() {}; 
    explicit A(bool b) {}; 
    A(BOOST_RV_REF(A)) {}
    A& operator=(BOOST_RV_REF(A)) { return * this; }

private:
    // Non-copyable
//    A(const A&);
//    const A& operator= (const A&);
};


int main()
{
    boost::container::map<int, A> myMap;
    myMap.emplace(1, true); // Works

    A a;
    myMap.emplace(1, boost::move (a));
//  myMap.emplace(1);       // Fails

}
4
  • Thank you, John. Unfortunately, I forgot to mention that the "A" class is not under my control.
    – Doodler
    Jan 29, 2014 at 18:02
  • @Doodler: Oh, my goodness. Now that does complicate matters somewhat. Jan 29, 2014 at 18:03
  • Actually, the solution should be simple: piecewise_construct was designed for this (among other things). See, however, my answer for the prelimary results.
    – sehe
    Jan 29, 2014 at 22:35
  • At this point I believe John's answer is the most right one because it's at least a solution for the problem as I formulated it formerly (with a touchable A class).
    – Doodler
    Jan 30, 2014 at 15:31
1

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;
stdMap.emplace(std::piecewise_construct, 
     std::forward_as_tuple(1), 
     std::forward_as_tuple());

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 { 
       std::piecewise_construct, 
       std::forward_as_tuple(1), 
       std::forward_as_tuple(true) 
};

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.

3
  • 1
    But std::piecewise_construct is not available on C++03 and I have not found a replacement for it in Boost. Now I believe it can't really be done in m scenario.
    – Doodler
    Jan 30, 2014 at 15:28
  • @Doodler I would concur. You could play very dirty tricks doing placement new in the value object. However, it's probably a lot easier to create a strong type around A that allows 'explicit' default construction overload... Of course this being C++03 this would include manually exposing the "inherited" constructors (since neither variadics or inherited ctors can be employed). Not fun.
    – sehe
    Jan 30, 2014 at 16:11
  • After having found boost::unordered::piecewise_construct your answer makes full sense, so I'm marking it now as the right one.
    – Doodler
    Feb 20, 2014 at 15:11
0

piecewise_construct support was added recently for Boost 1.62. So the following code:

#include <boost/container/map.hpp>
#include <boost/tuple/tuple.hpp>

// This class belongs to a third-party SDK and cannot be touched
class A
{
   public:
   explicit A(){}
   explicit A(bool b);

   private:
   // Non-copyable
   A(const A&);
   const A& operator= (const A&);
};

int main()
{
   using namespace boost::container;
   map<int, A> myMap;

   myMap.emplace(piecewise_construct, boost::tuple<int>(1), boost::tuple<>());

   return 0;
}

should work in C++03 and C++11+ compilers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.