Тёмный

C++ Weekly - Ep 447 - What Are Reference Qualified Members? 

C++ Weekly With Jason Turner
Подписаться 113 тыс.
Просмотров 7 тыс.
50% 1

Опубликовано:

 

24 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 31   
@MattGodbolt
@MattGodbolt День назад
I've used this on "builder" type objects where I build up by calling methods on some kind of factory thing (that maybe builds up vectors, unique_ptrs etc) and then to "build" I make it an `&&` r-value function that then can move all the configured pieces into a single, final object. Then one has to call `std::move(builder).build();` which makes it clear(ish) that the builder is now "finished"
@dreum99
@dreum99 День назад
I've played with similar concepts. The trick I used is that the "builder" is only available through a factory method and it's return in an "RvalueOnly" wrapper object. All the builder calls are forwards via an overloaded "operator->" that is an r-value function, so you're never allowed to get an lvalue version of the builder. This does make it so you have to chain all the calls together which you may not want, but it doesn't require a std::move, which feels unintuitive to me. Feel free to disagree though.
@japedr
@japedr День назад
If I'm not mistaken, for one-off building you can achieve the same effect by constructing the builder in place, e.g. Builder{}.x(1).y(2).build(). (build() is called with an r-value reference of the builder object).
@AlfredoCorrea
@AlfredoCorrea 20 часов назад
I think builder objects make sense if they are doing non-trivial work as they are build in steps (including checking arguments). Otherwise a skeleton object with just parameters is enough {.x = 1, .y = 2}. i could be missing something though. in any case, moving “at the end” is a great idea.
@friedkeenan
@friedkeenan День назад
You can also have volatile-qualified member functions, isn't that super fun and awesome
@saulth
@saulth День назад
I've used this on a tensor class for the assignment operator. The lvalue assignment is just the default of assigning a different tensor object to the value, but assignment to an rvalue meant broadcast assignment into the tensor, allowing a slice operator to return a temporary tensor that shared the original memory and then broadcast assignment into that temporary slice.
@KhalilEstell
@KhalilEstell День назад
I... did not know that this existed. Neat! Good to know.
@cactus-man
@cactus-man 16 часов назад
Same... And I've been using C++ professionally for ~3 years now. Guess it's time to go back to basics and see what else I missed...
@alskidan
@alskidan День назад
I used reference qualified functions extensively to implement a builder DP. It allowed me to get rid of the explicit .build() at the end. Made everything much much more readable.
@alskidan
@alskidan День назад
Oops, Matt wrote the same message before me. This is what one gets for not reading the comments. Well, never mind.
@wissensfrosch
@wissensfrosch День назад
I think the main problem to be solved here is that getters often return an l-value reference to the member for performance reasons or in the case of a non-const member to have a non-const l-value reference to be able to change the member (one special form of getters is the index operator). The problem now occurs if you do this on a temporary object. Overloading the member function for r-values helps to prevent this. (I believe that by now a reference to a member extends the lifetime of the temporary object. Then, much of what I said is not an issue anymore.)
@simonmaracine4721
@simonmaracine4721 День назад
Cool stuff!
@VladykaVladykov
@VladykaVladykov День назад
Всегда рассказываю об этом студентам, не понимаю, почему об этом так мало говорят! Наверное, так же редко, как о указателях на член класса, типа `int Car::* pSpeed = &Car::speed;`
@cactus-man
@cactus-man 16 часов назад
As a C++ dev you must know that explicitness is the most important thing there is. Your comment seems pretty comprehensible after translation, so why don't you just use any translator available to you (if you're too lazy to translate your comment yourself) to provide an explicit translation of your thoughts instead of relying on implicit auto-translated option?
@PaulMetalhero
@PaulMetalhero День назад
This is great. But for the && overload, I think it should be better to call it something like 'take_ownership', to make it more expressive
@MartineCozzi
@MartineCozzi День назад
037 Clifford Underpass
@MarcelRobitaille
@MarcelRobitaille День назад
I didn't know you could std::move a const. Doesn't that break const correctness? It leaves the variable in a moved from state.
@evo6789
@evo6789 День назад
No, std::move is kind of a misnomer. It doesn't actually move anything, it just does a static cast to T&&. So if you std::move a const object it will just return the object as const T&& but the internal state will not have changed at all by std::move
@avramlevitter6150
@avramlevitter6150 День назад
@@evo6789 And, pretty importantly, if you have a copy constructor as well as a move constructor (that takes a non-const rvalue) the copy constructor will be valid (rvalue -> lvalue) while the move constructor won't (because the const can't be removed). So you end up with a copy.
@MarcelRobitaille
@MarcelRobitaille День назад
@@evo6789 You're right. So it would be impossible to do `std::move(obj).set_value(...)`, but `std::move(obj).get_value()` is fine because it calls the `get_value() const &&` overload.
@obinator9065
@obinator9065 День назад
i hate water
@ChrisCarlos64
@ChrisCarlos64 День назад
It's fluid and wet and nourishing and it is everywhere!
@demenevdmitriy4175
@demenevdmitriy4175 День назад
You're 70% that.
@avramlevitter6150
@avramlevitter6150 День назад
1:55 I was just saying to myself that this would be a great time to mention deducing this, and then you anticipated me a little *too* well
Далее
Microservices are Technical Debt
31:59
Просмотров 194 тыс.
Should I pass by const reference or by value?
10:45
Просмотров 104 тыс.
НОВЫЙ РОЛИК УЖЕ НА КАНАЛЕ!
00:14
Просмотров 599 тыс.
LINQ's INSANE Improvements in .NET 9
11:26
Просмотров 32 тыс.
LiveView JS: The Comprehensive Guide
25:42
Просмотров 1,3 тыс.
Avoid These BAD Practices in Python OOP
24:42
Просмотров 53 тыс.
House Scares Patient Into A Stroke | House
10:11
Просмотров 62 тыс.
This experiment confirmed quantum physics
25:56
Просмотров 14 тыс.
This Lawsuit Could Change Science
7:58
Просмотров 210 тыс.
C++ Weekly - Ep 445 - C++11's thread_local
10:31
Просмотров 9 тыс.