r/ProgrammerHumor • • 13d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

143

u/geekosas 12d ago

Why waste 7 bits?

30

u/PastaPuttanesca42 11d ago

Because when I write a function template

foo<T>

that internally does

std::vector<T> bar;
[stuff happens]
T* ptr = &bar[7]

foo<bool> is broken.

-18

u/geekosas 11d ago

Man Up

17

u/PastaPuttanesca42 11d ago

What kind of response is that? Do you actually have anything to say?

26

u/SchighSchagh 12d ago

Right? And you can always use uint8_t if you have an actual reason.

65

u/Still_Bit_7527 12d ago

Because i asked for a vector of bool, not vector of bits

26

u/geekosas 12d ago

This is the kind of guy that programed Google Chrome XD

15

u/Rainmaker526 12d ago

Then ask for a vector of bytes.

28

u/Still_Bit_7527 12d ago

Bool is a byte. At minimum. All types are min 1 byte, by standard. And sizeof returns integers. Please stop arguing and just accept a common issue. Dont be a contrarian

3

u/geekosas 12d ago

Look a python developer 

13

u/Still_Bit_7527 12d ago

Java*. Compiler can optimize how it wants but documentation and consistency comes first

0

u/[deleted] 12d ago

[removed] — view removed comment

10

u/_killer1869_ 12d ago

A bool, by definition, in just about any language there is, always takes up the amount of space of the lowest addressable memory unit, which is 8 Bit (excluding some special, outdated architectures where you can access single bits directly). C++ is the only language I know of where a data type using bool uses single bits for storing them. Is it more efficient? Yes. But it breaks the universally accepted standard. Consistency is far more important than minimal efficiency improvements. Just add a seperate data type for when you really need that efficiency into the standard library.

5

u/Still_Bit_7527 11d ago

It's even worse. C++ itself actually uses a whole byte for bools. It's only vector that uses bits to optimize when it's a vector of bools

-2

u/geekosas 11d ago

Standard created by Samsung and HK hynx?

Like when pharma diagnosed 50% of kids with ADHD.