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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:11:41+00:00 2026-06-08T19:11:41+00:00

From the cpp documentation for std::vector , I see this: void push_back ( const

  • 0

From the cpp documentation for std::vector, I see this:

void push_back ( const T& x );

I understand that push_back makes a copy of the object that I pass. But, why is the signature const T& ? By looking at this, I initially thought it takes a const reference of whatever object that I push to the vector.

  • 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-08T19:11:42+00:00Added an answer on June 8, 2026 at 7:11 pm

    The other option would be

    void push_back(T x);
    

    that is, taking x by value. However, this would (in C++03) result in creating an extra copy of x (the copy in the arguments to push_back). Taking x by const reference avoids this.

    Let’s look at the stack for a call v.push_back(T()) taken by value:

    v.push_back(T());                      // instance of T
    void std::vector<T>::push_back(T x)    // copy of T
    new (data_[size_ - 1]) T(x)            // copy of copy of T
    

    Taking by const reference we get:

    v.push_back(T());                             // instance of T
    void std::vector<T>::push_back(const T &x)    // const reference to T
    new (data_[size_ - 1]) T(x)                   // copy of T
    

    In C++11 it would be possible (though unnecessary) to take x by value and use std::move to move it onto the vector:

    v.push_back(T());                             // instance of T
    void std::vector<T>::push_back(T x)           // copy of T
    new (data_[size_ - 1]) T(std::move(x))        // move the copy of T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code that need to be translated from C to Cpp, and
from this site there is header compress.h http://www.fredosaurus.com/notes-cpp/examples/compress/compress.html i am using visual c++ 2010
g++ -std=gnu++0x main.cpp In file included from main.cpp:6:0: CustArray.h: In constructor 'CustArray::CustArray()': CustArray.h:26:32: error:
I want to use some functions from a .cpp source file that has a
I have a C++ class that I recently renamed from *.cpp to *.mm to
Following example from this link: http://developer.kde.org/documentation/books/kde-2.0-development/ch03lev1sec3.html #include <QObject> #include <QPushButton> #include <iostream> using namespace
I'm trying to compile this little piece of code from the boost documentation: (http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/tutorial/filter_usage.html)
Do I have to put code from .cpp in a namespace from corresponding .h
I am trying to run blocking_udp_echo_server.cpp from Boost asio example on MacOSX 10.5. But
I have a cpp file from which I am generating a shared library (using

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.