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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:53:47+00:00 2026-06-18T07:53:47+00:00

Long story short, I have #include <vector> template <class T> class wrapped_vector { private:

  • 0

Long story short, I have

#include <vector>

template <class T>
class wrapped_vector {
private:
        std::vector<T> elements;
public:
        wrapped_vector() {
                elements.resize(20);
        }

        T& operator[](int i) {
                return elements[i];
        }

        const T& operator[](int i) const {
                return elements[i];
        }
};

int main(void) {
        wrapped_vector<int> test_int;
        test_int[0] = 1;

        wrapped_vector<bool> test_bool;
        test_bool[0] = true; // remove this line and it all compiles
}

and it gives me the compile error

test.cpp: In instantiation of ‘T& wrapped_vector<T>::operator[](int) [with T = bool]’:
test.cpp:28:13:   required from here
test.cpp:15:34: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool, std::allocator<bool> >::reference {aka std::_Bit_reference}’
  • 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-18T07:53:48+00:00Added an answer on June 18, 2026 at 7:53 am

    You got bitten by yet another side-effect of the “magic” std::vector<bool>.

    Since std::vector<bool> doesn’t actually store a contiguous array of bools, but packs them as a bitset, it cannot return a “real” reference to a bit in the middle of the bitset (since bits aren’t directly addressable); for this reason, its operator[] returns a proxy object that, overloading its operator=, “fakes” a reference semantic.

    The problem lies here: this proxy object is not a bool &, so you cannot return it as such in your method.

    The simplest way to solve would be something like this:

        typename std::vector<T>::reference operator[](int i) {
                return elements[i];
        }
    
        typename std::vector<T>::const_reference operator[](int i) const {
                return elements[i];
        }
    

    that guarantees that you actually return whatever type std::vector uses as “reference to T” in its methods.

    Also, you may want to use std::vector<T>::size_type for indexes (mostly for coherency of your forwarder functions than anything else).

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

Sidebar

Related Questions

Long story short Say I have the following code: // a class like this
To make a long story short I have to use processing in a class
Long story short, my question is: How can I force GnuPG which private/public key
Long story short, I have an ASP.NET application I'm trying to debug and at
So long story short, i have some forms (2) of them that, need different
Long story short, I'm developing a theme template for a blog that enables you
So long story short, I have some odd bugs in some code I'm messing
Ok - long story short. Have an e-comm site hosted on Windows VPS using
I have some markup which contains the firebug hidden div. (long story short, the
So long story short I have an object model in which several different entity

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.