mercredi 24 avril 2019

construct std::pair in-place in vector::emplace_back

I have a class A defined like bellow:

class A
{
public:
   A() = default;

   explicit A(uint32_t a, uint32_t b)
   {
      std::cout << "construct" << std::endl;
   }

   A(const A& obj)
   {
      std::cout << "copy" << std::endl;
      *this = obj;
   }

   A(const A&& obj)
   {
      std::cout << "move" << std::endl;
      *this = obj;
   }

   A& operator=(const A& obj)
   {
      std::cout << "copy operator" << std::endl;
      return *this;
   }

   A& operator=(const A&& obj)
   {
      std::cout << "move operator" << std::endl;
   }
};

I use the class like this:

std::vector<std::pair<A, bool>> v;
v.emplace_back(A(0, 1), true);

The emplace_back has the following output:

construct
move
copy operator

My question is, is there any way to construct A of the pair in-place without calling the move and copy operator?

Aucun commentaire:

Enregistrer un commentaire