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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:34:37+00:00 2026-06-14T12:34:37+00:00

The following is bad: vector<const int> vec; The problem is that the template type

  • 0

The following is bad:

vector<const int> vec;

The problem is that the template type needs to be assignable. The following code compiles [EDIT: in Visual Studio 2010], demonstrating a problem with the above:

vector<const int> vec;
vec.push_back(6);
vec[0] += 4;

With more complicated types, this can be a serious problem.

My first question is whether there is a reason for this behavior. It seems to me like it might be possible to make const containers that disallow the above and non-const containers that allow it.

Second, is there a way to make containers that function in this way?

Third, what is actually happening here (with a user type)? I realize it is undefined behavior, but how is the STL even compiling this at all?

  • 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-14T12:34:39+00:00Added an answer on June 14, 2026 at 12:34 pm

    The reason std::vector<T const> isn’t allowed is that the object in a vector may need to be reshuffled when inserting at a different place than the beginning. Now, the member std::vector<T>::push_back(T const& v) is conceptually equivalent to (leaving the allocator template parameter out as it is irrelevant for this discussion)

    template <typename T>
    void std::vector<T>::push_back(T const& v) {
        this->insert(this->end(), v);
    }
    

    which seems to be how it is implemented on some implementations. Now, this operation would requires, in general, that some objects might need to be moved and, thus, the T argument needs to be assignable. It seems that the standard library shipping with MSVC++ doesn’t delegate the operation but does all the necessary handling, i.e., resizing the array and moving the objects appropriately when running out of space, in push_back(). It isn’t quite clear what the requirements are on the type T to be able to use push_back().

    In principle, a container supporting both T const and an insert() operation in the middle would be possible, though: Nothing requires the internal storage to be T rather than typename std::remove_const<T>::type while exposing a T& in the interface. It is necessary to be a bit careful about the const-version ofoperations like operator[]() because just using T const& as the return type when T is some type S const would result in a type S const const. In C++ 2003 this would be an error, in C++ 2011 I think the const are just collapsed. To be safe you could use typename std::add_const<T>::type&.

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

Sidebar

Related Questions

I'm using the following code to update a record. Bad thing is that it's
Why is it that JSLint returns a 'Bad escapement' on the following JavaScript line
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
first let me say I know the following code will be considered bad practices..
Is the following code bad practice? for i in some_values: do_whatever(i) do_more_things(i) Somehow, it
Consider following piece of code void foo( bool forwad ) { vector<MyObject>::iterator it, end_it;
I have the following code for a vector of vectors of integers (i.e. an
Problem Consider the following class template <typename T> struct A { T x; };
Consider the following: std::vector<std::unique_ptr<int>> ptrsToInts; ptrsToInts.emplace_back(new int); If reallocation occurs in the vector, and
why is doing the following so bad? String val = null; String someOtherValue =

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.