307
I love Rust (lemmy.blahaj.zone)
submitted 1 week ago by rustyfemboy to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] flying_gel@lemmy.world 33 points 1 week ago

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;

[-] xor 43 points 6 days ago

It's nice that this exists these days, but my god is it horrendously unreadable at a glance

[-] Ephera@lemmy.ml 5 points 6 days ago

It makes it look like they're just adding random noise to avoid colliding with existing syntax. Maybe they can try a UUID next time...

[-] flying_gel@lemmy.world 5 points 5 days ago

It makes perfect sense actually. I did write another comment here if you are interested.

This is how operator overloads were written going back to the initial version of C++ back in 1985. The only new thing is that we can now add = default to get the compiler to generate a default implementation that compares all the member variables for you.

[-] GetOffMyLan@programming.dev 29 points 6 days ago

That is completely incomprehensible lol

[-] tetris11@lemmy.ml 19 points 6 days 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.

[-] flying_gel@lemmy.world 6 points 5 days 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&)

[-] qaz@lemmy.world 3 points 6 days ago* (last edited 6 days ago)

Is there a way to avoid having to write copy and move twice every time yet?

[-] flying_gel@lemmy.world 1 points 5 days ago

You mean copy/move constructor and assignment operator?

Unless you have any special handling the ones generated by the compiler automatically should work just fine. But if you do have to define them for some reason (which is becoming increasingly rare) you would need to define both if you need both copy/move construction and copy/move assignment.

[-] 2xsaiko@discuss.tchncs.de 1 points 6 days ago

Whoa nice, I need to keep this in mind.

[-] riodoro1@lemmy.world 4 points 1 week ago
[-] MyNameIsRichard@lemmy.ml 3 points 1 week ago

Only if you're a bad programmer :/

[-] GetOffMyLan@programming.dev 33 points 6 days ago

This argument just doesn't hold up. Software written by some of the best developers in the world still has these same bugs.

Why even use a language where you have to put so much effort into something that comes for free in many modern languages.

this post was submitted on 15 Nov 2024
307 points (100.0% liked)

Programmer Humor

32536 readers
480 users here now

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

Rules:

founded 5 years ago
MODERATORS