Skip to content

Instantly share code, notes, and snippets.

@Zekfad
Last active January 4, 2022 22:56
Show Gist options
  • Save Zekfad/26d6ac858d9548145a5baed5898c62d6 to your computer and use it in GitHub Desktop.
Save Zekfad/26d6ac858d9548145a5baed5898c62d6 to your computer and use it in GitHub Desktop.
C++ const correctness note

Expression Explanation

Can change p?

Can change *p?

const X * p
X const * p

p points to an X that is const.

+

-

X * const p

p is a const pointer to an X that is non-const.

-

+

const X * const p
X const * const p

p is a const pointer to an X that is const.

-

-

const X& r
X const& r

r is a reference to an X that is const.

It means r aliases an X object, but you can’t change that X object via r.

-

Not appliable

X& const r

Don't do it: it is nonsense.

r is a const reference to an X.

That is redundant — references are always const.

X& const r is functionally equivalent to X& r.

+

Not appliable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment