cpp-best-practices / cpp-best-practices/cppbestpractices

Clarify that marking certain member variables const can hinder performance

Open
#147 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
8.8k
Forks
902
PR merge metrics
No merged PRs in 30d

Description

In the 03-Style document, I read the following sentence:

> If the member variable is not expected to change after the initialization, then mark it const.

However, we know that if, for example, we mark a container as `const`, we prevent it from being moved, possibly hindering performance. Marking fields as `const` is useful to enforce invariants; however, it "interferes" with performance. If both const correctness and performance are important, one could leave the field non-const, make it private and define a field getter.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.