Skip to content

Remove const where std::move is used to promote moving. #13338

@Darshan-upadhyay1110

Description

@Darshan-upadhyay1110

This is an Easy Hack.
Potential mentors: @Ashod @Darshan-upadhyay1110

Identify places where std::move is applied to const variables. Moving from a const object does not provide a real performance benefit because it cannot steal the internal state – it falls back to a copy. Where safe and appropriate, remove the const qualifier to enable real move semantics.

Example

Before (sub-optimal, still copies):

const std::string name = "Document";
auto title = std::move(name); // still copies because `name` is const

After (allows move):

std::string name = "Document";
auto title = std::move(name); // moves from `name`

In some cases, removing const is not appropriate (for example, if the value must not change or is shared). Only update when it's clearly safe.

Good rule of thumb

  • Remove const only when

    • the variable is local
    • safe ownership is clear
    • and it is not used after the move
  • Do not remove const when

    • meaningfully immutable
    • referenced later
    • part of shared state
    • or a const reference / const member

Short contributor note to include

Only remove const if the variable is a local, owned value not used after the move. Never change API signatures, const references, config/state objects, or shared/global values.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions