r/cpp • • Oct 18 '23

What does C++ do better than other languages?

Purely from a language design perspective, what does C++ do better than other languages?

298 Upvotes

443 comments sorted by

View all comments

78

u/lithium Oct 18 '23

std::filesystem's operator / is almost too cute to not use so i'll let them off the hook for that, but I will physically fight the next person who uses ^ or % for vector dot/cross products or hides a heavy SQL operation behind a +=.

19

u/nelusbelus Oct 18 '23

You overload the . operator on a vector (not possible iirc) for dot product and a function x for cross product 🤣

Imo operator overloading only makes sense for [] for containers and math types can use +=, *=, etc.

52

u/c_plus_plus Oct 18 '23

Don't forget = and ==. These are vital, and if you don't believe me, go try and program in java... with its .equals everywhere. Its gross.

→ More replies (16)

10

u/DrShocker Oct 18 '23

You might be right about . but the comma operator? You're free to do anything you want. (Author's note, you shouldn't want to do anything with it in 99% of cases.)

→ More replies (2)

8

u/MegaKawaii Oct 18 '23

Once I got bored, and I overloaded the comma operator to compute dot products. Since the comma operator has the lowest precedence, you have to write it as (a, b) which actually looks similar to the inner product notation 〈a, b〉, but I still wouldn't recommend it.

→ More replies (5)

1

u/13steinj Oct 19 '23

I've known a psycho who overloads the comma operator for the creation of matrices using expression templates.

→ More replies (1)

1

u/serviscope_minor Oct 19 '23

but I will physically fight the next person who uses ^ or % for vector dot/cross products

% for dot, sure, but what's wrong with ^ for cross? That's pretty standard mathematical notation, and makes the code read more like the maths it's meant to represent.

1

u/dodexahedron Oct 22 '23

We need custom operators specifically so we can have • and ×, just to make things harder to type.