this post was submitted on 15 Nov 2024
262 points (93.1% liked)

Programmer Humor

32476 readers
698 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 32 points 1 day ago (13 children)

I do appreciate how newer C++ standards have made these kinds of things a lot easier too.

Define all comparison operators with just one one line using C++20

auto operator<=>(const ClassName&) const = default;

[–] [email protected] 27 points 1 day ago (2 children)

That is completely incomprehensible lol

[–] [email protected] 3 points 5 hours ago

Maybe to a non C++ dev, but a lot of C++ is probably incomprehensible to a non C++ dev, just like there are other laguages that are incomprehensible to C++ devs. To me it makes perfect sense as it works just like all the other operator overloads.

auto - let the compiler deduce return type

operator<=> - override the spaceship operator (pretty sure it exists in python too)

(const ClassName&) - compare this class, presumably defined in Class name, with a const reference of type Class name, i.e. its own type.

const - comparison can be made for const objects

= default; - Use the default implementation, which is comparing all the member variables.

An alternate more explicit version, which is actually what people recommend:

auto operator<=>(const ClassName&, const ClassName&) = default;

if I just want to have less than comparison for example I would:

This one makes it explicit that you're comparing two Class name objects.

if I just want to have less than comparison for example I would:

auto operator<(const ClassName&, const ClassName&) = default;

If I need to compare against another class I could define: auto operator<(const ClassName&, const OtherClass&)

[–] [email protected] 20 points 1 day ago

You just need to break the syntax apart and look at it from the LHS and the RHS seperately.

In layman's terms: constantine felt boxed in by his social class which left him often at dagger-ends to the operations on his car. Unable to keep up with the constant payments, he defaulted on the loan.

See? Easy.

load more comments (10 replies)