C++ Weekly - Ep 225 - Understanding Operator Overloading

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

The operator << for ostream is basically the poster child of operators you want to overload.

Tyranisaur
Автор

Just first time realize that the "Hello, ..., on-site training" monologue is probably not prerecorded.

LesleyLai
Автор

11:30 Haha nice.

By the way, this could have been a nice intro to implementing smart pointers or std::integral_constant.

Some further points could include: literal overloading, type overloading and the Return Type Resolver Idiom.

akj
Автор

This is the first time I'm not convinced of the message of this "primer". It is technically not that difficult to overload operators. But like you, @Jason, explicitly stated … be aware to do the right thing only the right … without telling something about how to do the right decisions in necessary or needed or good operator overloading.
I personally need a guideline about when and why and not technically how!

MartinDE
Автор

Whoa Jason, upping your frame transition game I see

warrenbuckley
Автор

I have a couple of questions:

1 - At 5:58, why is there an implicit conversion depending on the rhs/lhs order?
Is it because of some kind of parsing rules?
If I understand it correctly, the expression:
c+4.5
Gets implicitly converted via constructor call, as if it were
c+Complex{4.5}
So I would expect that
4.5 + c
Gets converted into
Complex{4.5} + c
But it doesn't.

2 - At 6:47, is "static" instead of "friend" possible for operator+? If so, is there a difference?
Also, is there a difference between friend function definition inside class declaration (like in the example) and friend definition outside it (I understand that there has to be a forward declaration anyways)?

Thanks for your videos. They are quite helpful.

japedr
Автор

I think the only time I can ever claim to know more C++ than Jason was the ordering of that [[nodiscard]]! 😂 Not much of a brag but it's probably the best I'll ever get. Very interesting video is there any way of making it so the ordering of operator args redundant. Usually the ordering of operator arguments is very important but it would seem that a + generally for many types should be sorta bidirectional. e.g. complexB = complexA + doubleA is not the same as complexB = doubleA + complexA.

fredhair
Автор

Who, on the standard committee allowed overloading "/=" for path concatenation in std::filesystem:: path?

homomorphic
Автор

C++ really has bad readibility. [Nodiscard] friend constexpr....

LeoHsieh
Автор

For the pointer example, which operator overload would be called? The const or non-const one?

Clouds
Автор

The friend function version looks weird. Should this even be allowed? Isn't the spaceship operator in C++20 not a better option?

patlecat
Автор

A lot of people get mad at C++'s (and thus blame C++) operator overloading because of people making stupid overloads, which annoys me because the language shouldn't be blamed for people doing stupid things, you can do stupid crap in every language, just because C++ allows you to do a lot of stupid things if you're feeling stupid doesn't mean its the language's fault.

Spartan
Автор

5:41 this behavior will change in C++20. Funny enough, the code will still not compile, but for a different reason (small `const` will fix that)

KennyMinigun
Автор

I hope in C++23, operator+ and operator- will be defaulted for struct like this

EpicSnakes
Автор

There were 5 dislikes for this video at the time of this comment. Why??

ameyabhurke
Автор

wow, you are really ONE QUICK TYPIST....

plushsu
Автор

I want to make a AI like Jarvis but I don't know how I love this language

cpubug