Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:16:21+00:00 2026-06-14T22:16:21+00:00

I was trying to adapt some code and moving the content from a vector

  • 0

I was trying to adapt some code and moving the content from a vector to another one using emplace_back()

#include <iostream>
#include <vector>

struct obj
{
  std::string name;

  obj():name("NO_NAME"){}
  obj(const std::string& _name):name(_name){}

  obj(obj&& tmp): name(std::move(tmp.name)) {}
  obj& operator=(obj&& tmp) = default;

};

int main(int argc, char* argv[])
{

  std::vector<obj> v;
  for( int i = 0; i < 1000; ++i )
  {
    v.emplace_back(obj("Jon"));
  }

  std::vector<obj> p;
  for( int i = 0; i < 1000; ++i )
  {
    p.emplace_back(v[i]);
  }

  return(0);
}

This code doesn’t compile with g++-4.7, g++-4.6 and clang++: what it’s wrong with it ?

I always got 1 main error about

call to implicitly-deleted copy constructor of obj

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:16:22+00:00Added an answer on June 14, 2026 at 10:16 pm

    Although the existing answer provides a workaround using std::move that makes your program compile, it must be said that your use of emplace_back seems to be based on a misunderstanding.

    The way you describe it (“I was trying to […] moving the content from a vector to another one using emplace_back()“) and the way you use it suggest that you think of emplace_back as a method to move elements into the vector, and of push_back as a method to copy elements into a vector. The code you use to fill the first instance of the vector seems to suggest this as well:

    std::vector<obj> v;
    for( int i = 0; i < 1000; ++i )
    {
      v.emplace_back(obj("Jon"));
    }
    

    But this is not what the difference between emplace_back and push_back is about.

    Firstly, even push_back will move (not copy) the elements into the vector if only it is given an rvalue, and if the element type has a move assignment operator.

    Secondly, the real use case of emplace_back is to construct elements in place, i.e. you use it when you want to put objects into a vector that do not exist yet. The arguments of emplace_back are the arguments to the constructor of the object. So your loop above should really look like this:

    std::vector<obj> v;
    for( int i = 0; i < 1000; ++i )
    {
      v.emplace_back("Jon");   // <-- just pass the string "Jon" , not obj("Jon")
    }
    

    The reason why your existing code works is that obj("Jon") is also a valid argument to the constructor (specifically, to the move constructor). But the main idea of emplace_back is that you need not create the object and then move it in. You don’t benefit from that idea when you pass obj("Jon") instead of "Jon" to it.

    On the other hand, in your second loop you are dealing with objects that were created before. There is no point in using emplace_back to move objects that exist already. And again, emplace_back applied to an existing object does not mean that the object is moved. It only means that it is created in-place, using the ordinary copy constructor (if that exists). If you want to move it, simply use push_back, applied to the result of std::move:

    std::vector<obj> p;
    for( int i = 0; i < 1000; ++i )
    {
      p.push_back(std::move(v[i]));  // <-- Use push_back to move existing elements
    }
    

    Further notes
    1) You can simplify the loop above using C++11 range-based for:

    std::vector<obj> p;
    for (auto &&obj : v)
      p.push_back(std::move(obj));
    

    2) Regardless of whether you use an ordinary for-loop or range-based for, you move the elements one by one, which means that the source vector v will remain as a vector of 1000 empty objects. If you actually want to clear the vector in the process (but still use move semantics to transport the elements to the new vector), you can use the move constructor of the vector itself:

    std::vector<obj> p(std::move(v));
    

    This reduces the second loop to just a single line, and it makes sure the source vector is cleared.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to adapt some code from a simple app that uses Raphael to
I'm trying to adapt some old code that uses NSZoneMalloc. The project I'm using
I am trying to adapt some (to me) very complicated code to work with
I'm trying to adapt some tornado code to work with twisted. Tornado's IOLoop has
We are trying to adapt to using widgets from jquery-ui for most of our
I'm currently trying to adapt some sample code and have hit a snag when
Im trying to adapt some code that I have found online that uses this
I am learning Java generics and I am trying to adapt some code I
I am trying to implement some C code in Java by using SWIG 1.3.
I'm trying to adopt some best practices to keep my python code efficient. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.