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

  • Home
  • SEARCH
  • 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 9214291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:56:36+00:00 2026-06-18T01:56:36+00:00

Possible Duplicate: Why vector<bool>::reference doesn’t return reference to bool? I used to think that

  • 0

Possible Duplicate:
Why vector<bool>::reference doesn’t return reference to bool?

I used to think that with std::vector::operator[] we get deep copies of the accessed item, but it seems that it is not always true. At least, with vector<bool> the following test code gives a different result:

#include <iostream>
#include <vector>
using namespace std;

template <typename T>
void Test(const T& oldValue, const T& newValue, const char* message)
{
    cout << message << '\n';

    vector<T> v;
    v.push_back(oldValue);
    cout << " before:  v[0] = " << v[0] << '\n';

    // Should be a deep-copy (?)       
    auto x = v[0];   
    x = newValue;

    cout << " after:   v[0] = " << v[0] << '\n';
    cout << "-------------------------------\n";
}

int main()
{
    Test<int>(10, 20, "Testing vector<int>");
    Test<double>(3.14, 6.28, "Testing vector<double>");
    Test<bool>(true, false, "Testing vector<bool>");
}

Output (source code compiled with VC10/VS2010 SP1):

Testing vector<int>
 before:  v[0] = 10
 after:   v[0] = 10
-------------------------------
Testing vector<double>
 before:  v[0] = 3.14
 after:   v[0] = 3.14
-------------------------------
Testing vector<bool>
 before:  v[0] = 1
 after:   v[0] = 0
-------------------------------

I would have expected that v[0] after the x = newValue assignment would still be equal to its previous value, but this seems not true.
Why is that?
Why is vector<bool> special?

  • 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-18T01:56:37+00:00Added an answer on June 18, 2026 at 1:56 am

    vector<bool>::operator[] neither yields a bool nor a reference to a bool. It just returns a little proxy object that acts like a reference. This is because there are no references to single bits and vector<bool> actually stores the bools in a compressed way. So by using auto you just created a copy of that reference-like object. The problem is that C++ does not know that this object acts as a reference. You have to force the “decay to a value” here by replacing auto with T.

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

Sidebar

Related Questions

Possible Duplicate: Reference collapsing? template< class T = const std::vector<int> & > void f(const
Possible Duplicate: Can I call functions that take an array/pointer argument using a std::vector
Possible Duplicate: How to downsize std::vector? It seems that std::vector will not release memory
Possible Duplicate: std::vector<std::string> to char* array I have to call a c function that
Possible Duplicate: C++ Vector Pointers to Objects Does std::list::remove method call destructor of each
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
Possible Duplicate: Erasing from a std::vector while doing a for each? I'm trying to
Possible Duplicate: STL vector and thread-safety If I have this piece of code: std::vector<std::vector<double>>
Possible Duplicate: std::vector resize downward If I resize() an std::vector to some size smaller
Possible Duplicate: Are std::vector elements guaranteed to be contiguous? does std::vector always contain the

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.