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 8857181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:31:47+00:00 2026-06-14T14:31:47+00:00

this code #include <iostream> #include <vector> struct obj { std::string name; int age; float

  • 0

this code

#include <iostream>
#include <vector>

struct obj
{
  std::string name;
  int age;
  float money;

  obj():name("NO_NAME"),age(0),money(0.0f){}
  obj(const std::string& _name, const int& _age, const float& _money):name(_name),age(_age),money(_money){}

  obj(obj&& tmp): name(tmp.name), age(tmp.age), money(tmp.money) {}
  obj& operator=(obj&&) {return *this;}

};

int main(int argc, char* argv[])
{
  std::vector<obj> v;
  for( int i = 0; i < 5000000; ++i )
  {
    v.emplace_back(obj("Jon", 45, 500.6f));
  }
  return(0);
}

is about 2 time slower than the equivalent with v.push_back(obj("Jon", 45, 500.6f)); and I don’t get why.

I have tested this with bot g++ 4.7.2 and clang++ 3.3.

Where I’m wrong ?


now that i have corrected my move construnctor i will add more

this is a push_back version

this is the emplace_back version

I’m testing this 2 with the time utility under Linux and compiling them with

g++-4.7 -std=c++11 -s -O3 -DNDEBUG

or

clang++ -std=c++11 -s -O3 -DNDEBUG
  • 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-14T14:31:49+00:00Added an answer on June 14, 2026 at 2:31 pm

    Not doing anything is better. You tried to make it faster (faster than what? did you actually profile before you wrote the move constructor?), but you broke it.

    The compiler generates copy and move constructors and assignment operators for free, and she does it right. By deciding to write your own, you are telling the compiler that you know better, so she just gets out of the way and lets you improve break it on your own.

    The first thing you broke, is that you made your move constructor actually copy. Things with a name are lvalues, and lvalues cannot be moved implicitly even if they are rvalue references. So the initializers need to actually call std::move.

    The second thing you broke is that you didn’t make the move constructor declare that it does not throw by adding noexcept to it. The compiler generated one had this. By not declaring that no exceptions are thrown, the implementation of std::vector will probably not use moves when reallocating the underlying storage: it cannot provide the strong exception guarantee without the assurance that moves don’t throw.

    Will doing all this make it perform better? Maybe. Maybe not. Your implementation may be doing the small string optimization on std::string, and that means that there is no dynamic allocation: the whole string "Jon", being small, will be stored directly in the std::string object. This makes a move have the same costs as a copy.

    You can make the whole obj structure take advantage of a cheap move by allocating it dynamically, and using unique_ptr. This will make moves cheaper than copies, even in the presence of the small string optimization. However, you are paying for that cheapness with the cost of allocation and extra indirection. Whether that is desirable or not only you can tell.

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

Sidebar

Related Questions

For the following code: #include<iostream> #include<vector> #include<string> using namespace std; struct Test { string
I've got this code: #include <iostream> #include <string> #include <vector> using namespace std; vector<string>
When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
I have this code: #include <iostream> #include <functional> struct A { int operator()(int i)
This is the C++ code: #include<iostream> using namespace std; int a=8; int fun(int &a)
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
#include <iostream> #include <string> #include <vector> using namespace std; struct Exmpl{ Exmpl() { cout
Consider the following code: #include <algorithm> #include <iostream> #include <vector> struct A { int
The code is following: #include <iostream> #include <vector> #include <algorithm> using namespace std; struct
#include <iostream> #include <vector> using namespace std; struct s_Astruct { vector <int> z; };

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.