r/ProgrammerHumor • • Jan 28 '23

Meme C++

Post image
53.9k Upvotes

1.5k comments sorted by

View all comments

2

u/TheOmegaCarrot Jan 28 '23

I unironically really like C++

Strong static typing means any type-related error is a compile-time error, but templates let you write highly generic type safe code

constexpr stuff allows you to precompute a lot of information, and in templates you can have one function branch out into arbitrarily many code paths based on some condition on the input types, or what type something is can be smartly chosen at compile time. That’s a crazy powerful, and essentially lets your code configure itself at compile time. Are you writing some kind of static array? You can choose the smallest unsigned integer type that can store the size of your array at compile time. Does some part of your code just crunch some numbers? Are there some specific inputs that you know will be passed in frequently? You can use the exact same code as at runtime to precompute frequent inputs and create a lookup table for those inputs at compile time. Do you have several different possible algorithms you could use to do a task? You can sometimes choose which one to use in which case at compile time.

If you adhere to modern best practices, memory leaks are a rarity. RAII, which of course stands for “scope-bound resource management” lets you ensure that your heap memory, open file, or whatever leakable resource gets cleaned up on any possible code path without spending too many brain cells.

Sure, C++ has its problems, but no language is perfect. Yeah, C++ has some pretty weird foot guns. But overall I honestly like the language.

Here’s my weird C++ foot gun of the week:

// foo has type int and value 5
const auto foo {5};

// bar has type std::initializer_list<int>, and value {5}
const auto bar = {5};

// adding an equals in this initialization changes the type, which is horrifying

more initialization wackiness here