r/ProgrammerHumor • • 10d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

265

u/Blue_Moon_Lake 10d ago

But because C++ never fixes past mistakes by breaking behavior, it'll never happen and they'll never do the sensible thing.

69

u/ImpulsiveBloop 10d ago

This is why I tend to just stick to C stdint types when I want specific data types while saving space. Consistent, configurable, and can be typedef-ed for clarity.

57

u/bwmat 10d ago

We just use vector<char> when we need it at work lol. 

45

u/Bloodgiant65 10d ago

Yeah I think that is pretty normal, unfortunately. Though to be explicit I would normally use uint8_t instead.

19

u/ImpulsiveBloop 10d ago

Yeah, I recall there being a difference between using char and uint8_t, too. But I can't be bothered to remember what. Overflow or something.

Unsigned char would be better. But that is more stuff to type out, and less clear on what it is imo.

26

u/nfitzen 10d ago edited 10d ago

From a C perspective, since IDK C++: char is defined to be the smallest addressable unit (in other words, a machine byte, which may be more than 8 bits on cursed hardware). Since CHAR_MAX >= 127 per the ISO C standard, it's probably the best thing to use for a hacky boolean type (of course, when bool doesn't work). uint8_t could also be used, but I feel like it communicates that we need 8 bits specifically rather than "the minimal amount the machine can address individually."

Edit: Also, unsigned char might be my first instinct, but it doesn't particularly matter. Any non-zero value is truthy to C. So I'd probably use char since I'm a fan of brevity.

7

u/ImpulsiveBloop 10d ago

Thank you. Just pretend I put in the effort to say this myself lol

6

u/bwmat 10d ago

If you're actually using the elements as booleans, only zero/non-zero matters, so char, signed char, unsigned char (yeah these are actually 3 separate types lol cpp) are all the same in this context

3

u/ImpulsiveBloop 10d ago

Yeah, I just meant in general. 

They are different, was my point, so they shouldn't be used interchangeably without consideration.

2

u/Scolmann 10d ago

Is there any behavior that breaks from them changing it to be bytes though?

It would take more memory but logically the code should be the same unless someone's doing bitcast BS with it

2

u/Blue_Moon_Lake 10d ago

It breaks multithreading.

6

u/TheThiefMaster 10d ago

No the current behaviour breaks multithreading

1

u/Blue_Moon_Lake 10d ago

Oh I read it incorrectly. I thought you asked what the bitset behavior was breaking.

Theoretically, it would break all code that try to handle it as a bitset by doing unsafe shitty read/write on the memory address.
But also, it would be extremely easy to do a "replace all" of vector<bool> to bitset when upgrading the code.

3

u/TheThiefMaster 10d ago

Can you manually read/write vector<bool>? I don't think it gives you data pointers at all

1

u/Blue_Moon_Lake 9d ago

Probably, but I never tried to write stupid code on purpose.

2

u/TheThiefMaster 9d ago

But my question highlights a problem with vector<bool> - it can't work in generic code that expects vector to be contiguous, addressable with T*, and have a .data() function that gives you that. Because it doesn't.

It doesn't even support &bool_vec[0] or &*begin() - because neither return actual references, but a reference proxy class. You can't take the address of an rvalue class, so it fails to compile entirely.

It's very well defended, but on the flip side it's incompatible with a lot of generic contexts because it doesn't have the same API as vector<T> generally, only a subset of it. Some of which seems to exist solely for there to be a common subset (like the nested reference type alias).

And the worst part is it explicitly has a member function on its nested "reference" type - so currently it can't be left unspecialised by an implementation.

1

u/Blue_Moon_Lake 9d ago

Yes, everybody says it should have been a different type while vector<bool> should be a regular vector.

4

u/TheThiefMaster 9d ago

And everyone knows it - but as long as any code uses vector<bool>::reference::flip() it's hard to fix.

IMO, the best solution is to make the new type, given the warning that change is coming, and then make the change. End the nonsense.

1

u/adabsurdo 9d ago

Well breaking backwards compatibility just for esthetics / marginally practical reasons would be very stupid

0

u/SmoothTurtle872 10d ago

Simple: Vector<ByteBool>