C++ is bad: structs aren’t the same as structs
Here’s a fun little problem. Suppose you’ve got a system that makes use of the following struct:
struct User { unsigned int user_id; unsigned short access_level; float account_balance; // stored as US dollars }
Your system is ridiculously well-tested, all the tests pass, everything works totally fine. I’ll be so bold as to say it’s (kinda) bug-free.
One day, you decide to add a new field to the User
struct, a string called name
. You recompile everything, and suddenly all kinds of tests fail! New users have all kinds of access levels, some have account balances in the billions while others have accounts worth negative billions. What happened, why, and how do you fix it (without removing name
again)? Edit: and no, the code does not rely on brittle assumptions about the value of sizeof(User)
, nor does it break type safety and have unsafe casts or void*
’s pointing into the middle of User
s. That would be too easy.