Skip to content

Change const T&& parameters to T&& to enable proper move semantics #29464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

Sea-n
Copy link
Contributor

@Sea-n Sea-n commented Jun 17, 2025

Replace const T&& parameters with correct T&& (non-const rvalue references) so that move() semantics actually take effect.

Problem

const T&& disables moves

Move constructors/operators are declared as widget(T&&) / widget& operator=(T&&). When a parameter is const T&&, the argument becomes an immutable rvalue, so the move constructor cannot be called, only the copy constructor is viable.

std::move() is pointless on const rvalues

std::move merely casts to exactly the same reference type it receives; if the variable is const, the result is still const T&&.

Unnecessary copies & misleading code

Large objects are copied instead of moved, wasting memory/CPU and hiding the real cost from programmers.

Fix

Drop the const qualifier on all rvalue-reference parameters:

 ItemImage::ItemImage(
- const QPixmap &&pixmap,
+ QPixmap &&pixmap,
  ItemBase::Data data)
 : ItemBase(std::move(data))
 , _pixmap(std::move(pixmap)) {
 }

Impact

  • Restores true move semantics, eliminating extra deep/shallow copies.
  • Reduces temporary allocations when loading images or constructing message items.
  • Aligns code intent with behavior, improving maintainability and readability.

References

@john-preston
Copy link
Member

Last one changes const lvalue reference to non const lvalue reference 🤔

@john-preston
Copy link
Member

And it even broke the build as you can see here:

https://github.com/telegramdesktop/tdesktop/actions/runs/15718851021/job/44295293711?pr=29464

Previously some constructors/functions used `const T&&`, which prevents
calling the move constructor. This commit removes the `const` qualifier
so that `std::move` actually performs a move.
@Sea-n
Copy link
Contributor Author

Sea-n commented Jun 18, 2025

Oh, thanks for pointing out rpl::start_with_next() takes const lvalue reference, which is different thing with this one. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants