This deep dive effectively exposes the technical debt of the 90s, proving that `std::vector<bool>` is neither a true vector nor a collection of booleans. It is a sharp reminder that in C++, premature optimization is often just another word for broken abstractions.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
A vector of bools is a vector of bools... right? | C++ Deep DiveAdded:
So, I was browsing Twitter a couple weeks ago and I came across this interesting tidbit in C++, which is that a vector of booleans is actually not a vector of booleans. Whenever you create a vector of booleans in C++, that gets transformed into bits. And the reason it's implemented like this is that in the '90s, the C++ committee wanted to showcase that C++ can do these sort of invisible optimizations that can automatically allow you to save RAM.
Because back then RAM was very expensive. Now, to be fair, in this day and age, RAM is also getting unusually expensive, but that's because of AI.
Anyway, point is, in the '90s, RAM was expensive and the C++ committee wanted to show that C++ can do stuff underneath the hood and so they created vector bool to not be a vector of booleans, but essentially a bits where the bit shifting math happens underneath the hood. So, you as a programmer don't worry about it, but you save a lot of memory. I just wanted to talk about this in this video and kind of showcase what that entails.
And in order to do this, I think it would be helpful to take a look at these two lines of code here. So, I've got a boolean A just set to true and a vector B of booleans, which has these values.
And let's take a look at how the vector of booleans is unexpectedly different from what one might expect a vector of booleans to be. And we can see that more clearly if we examine the memory.
A is a boolean set to true. If we examine the value at A, we'll notice that A is taking up 1 2 3 6 7 8. So, it's taking up 8 bits or 1 byte of memory. And this is essentially the core crux of the problem that the C++ committee wanted to solve. They said, "Hey, look, if you've got a boolean, it's a byte and you've got a vector of bytes like this, you're actually going to be wasting a lot of memory. You're going to be wasting 8x the memory that you don't need." So, what they decided to do was kind of convert this and represent all the data in bits. Let's see how that works.
So, I'm going to print B, which is the boolean vector that we've got. Now, what I really want to do is print what is at the address of where B is. In order to do this, I could do frame variable {dash} {dash} raw B. This is going to give us the address of B, which I can copy and I can do hex or {slash} x. And now you're going to see something interesting. You're going to see the value D. Now, D, in case you don't know, in binary, we should convert this to binary, right? Because the D doesn't make sense. But, if we print this in binary, we're going to see that this is exactly what we set our variable B to, but of course this is in little endian.
Don't worry about like the actual numbers, but essentially if I were to print the variable B, you're going to see I set it to true, false, true, true, and that's essentially what's being represented here.
But, notice that all these four values are actually packed into a single byte.
We are representing all of these values inside of this. So, if I had more values inside of B, maybe I had like two or three more, then they would be represented inside of this one byte as well. But, that was different from what we were seeing before when we tried to print out {ampersand} A. And here you're going to see that A, it has this one byte set to true, and essentially all of this is wasted memory. So, that's what the C++ committee was trying to do.
Honestly, this is like very interesting to me. They really tried to squeeze as much memory as they could out of C++, even though A is a boolean, it it's wasting seven bits of of data. But, in this boolean array, the C++ committee decided, "All right, well, why don't we save that space for the user internally and let them not have to worry about it." But, whenever you write code that deviates from the users' expectations, there's going to be some possible problems, and it's good to talk about what those are.
The easiest way to understand how a vector of bool kind of deviates from the norm is to take a look at how another type of vector does not deviate from the norm and one example would be using an integer. So, if you've got an integer like int c equals 123 and then you've got an int vector d and let's just say that you decide to do auto d0. If you're checking the size of the bytes for both of these, they should be four. The size of c, which is an integer, is four bytes and then the size of d0, which is also an integer, should be four bytes. That's not what actually ends up happening when you do this approach right here. The size of a, which is a boolean, is going to be one byte. You would expect that b0, when you're checking the zeroth element of b, to be one byte as well.
But, because C++ has implemented this sort of converting the vector of boolean into bits internally, it needs to return a proxy. And if you were to print this out, you're going to see that even though the size of a is one byte, the size of b0 is going to be much larger.
I'm going to do a make run. The random number and d0 are both four bytes long, but the byte of a is one, whereas the bytes for b0 is 16 and that is because we are returning a proxy, which is different from what we might expect b0 to be. We would expect it to be a boolean.
Now, there are a couple of other edge cases to account for. The first is that d0 is a copy. When we get the value inside of an integer, we're taking a copy of that integer. So, setting d0 equal to 10 is not going to change what's inside of d0, but that's not the case for our vector boolean. So, once again, this sort of deviates from the norm of what we would expect. We set this value, which we're doing auto b0 equals the zeroth element inside of b, because we're getting a reference for it instead of a copy, setting b0 to false here is also going to change this value right here to be false. It's not a copy, it's a reference.
So, if we actually wanted to just create a copy, we would have to write something like so, and this is going to implicitly cast the proxy into a Boolean, which of course is going to be a copy, so that we can set this to true, and this will not change. So, there's a couple of edge cases here that you need to consider when using a vector of Booleans.
And if you try to create a pointer for Boolean like this, or if you try to create a reference for Boolean like this, you're also going to run into some compiler errors, because that's also not possible.
The final thing to know is that it also has some issues with thread safety.
Let's say you've got this vector Boolean, you know, true, false, true, true. It's represented like so, right?
The 0xD actually stores the information for the four different Boolean values, because D in binary, we can see that it's 1101, and that's exactly what this array is. It's It's true, false, true, true. Because of that, it's also thread unsafe if you're trying to change the data in here and here in two different threads, all that data is stored in one byte.
If we were to summarize this video, I would say that the people who work on C++ tried to save RAM, because that was expensive back in the '90s, and there were some unforeseen issues that came along the way, and right, it's it's confused developers for 30 or so years, because personally, I would have not known that that's how the vector Boolean existed unless I saw a random meme on Twitter. So, because I saw the random meme, I learned about it, and then now I know. But, if I had not seen that meme, that that thought would have never registered for me, right? the recommendation here is that, you know, if you actually want a vector of Booleans to behave like sort of the container that you might expect, then I think the recommendation is to use a vector of characters. So, yeah, that's what I want to talk about in this video.
A little bit of a long way to say that a vector of booleans is not actually a vector of booleans. It gets compressed into bits in the data. And yeah, that's all I want to talk about in this video.
Hopefully, that was helpful. Hopefully, you learned something interesting. And if you've made it this far, I'd appreciate it if you could just hit the like button and consider subscribing for more C++ deep dives just like this.
Thanks for watching and I'll catch you guys next time.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











