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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:43:34+00:00 2026-06-12T22:43:34+00:00

The Standard Template Library documentation for list says: void push_back ( const T& x

  • 0

The Standard Template Library documentation for list says:

void push_back ( const T& x );

Add element at the end Adds a new element at the end of the list,
right after its current last element. The content of this new element
is initialized to a copy of x.

These semantics differ greatly from the Java semantics, and confuse me. Is there a design principle in the STL that I’m missing? “Copy data all the time”? That scares me. If I add a reference to an object, why is the object copied? Why isn’t just the object passed across?

There must be a language design decision here, but most of the commentary I’ve found on Stack Overflow and other sites focuses on the exception throwing issues associated with the fact that all this object copying can throw exceptions. If you don’t copy, and just handle references all those exception problems go away. Very confused.

Please note: in this legacy code base I work with, boost is not an option.

  • 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-12T22:43:36+00:00Added an answer on June 12, 2026 at 10:43 pm

    The STL always stores exactly what you tell it to store. list<T> is always a list of T so everything will be stored by-value. If you want a list of pointers, use list<T*>, that will be similar to the semantics in Java.

    This might tempt you to try list<T&>, but that is not possible. References in C++ have different semantics than references in Java. In C++, references must be initialized to point to an object. After a reference is initialized, it will always point to this object. You can never make it point to a different object. This makes it impossible to have a container of references in C++. Java references are more closely related to C++ pointers, so you should use list<T*>, instead.

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

Sidebar

Related Questions

Is there a C++ Standard Template Library class that provides efficient string concatenation functionality,
I've recently started teaching myself the standard template library. I was curious as to
In Microsoft Visual C++ (VS 2008/2010) using a number of Standard Template Library containers
Declarations of auto_ptr from C++ Standard Library namespace std { template <class Y> struct
Currently I am studying Standard Template Library (STL). In this program I am storing
I mean, aside from its name the Standard Template Library (which evolved into the
In C++ Standard Template Library, there's a 'functional' part, in which many classes have
Anyone heard of a good standard template library in Ruby? Or any wrapper exists?
I'm wanting to become conversant in the use of the Standard Template Library. If
Suppose I am writing a template library consisting of a function template template<T> void

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.