East const
vs. const
West
East vs. West const
E/W const
two alternative styles — both correct two styles — both correct
East const
one consistent rule:
What's left of const
is constant.
const
West
(still) more widespread,
but less consistent
int i = 1;
int & r = i;
int * p = &i;
int const c = 1;
int const& cr = i;
// pointer to const int
int const* pc = &i;
// const pointer to int
int *const cp = &i;
// const pointer to const int
int const*const cpc = &i;
const int c = 1;
const int& cr = i;
// pointer to const int
const int* pc = &i;
// const pointer to int
int *const cp = &i;
// const pointer to const int
const int *const cpc = &i;
Constrained auto
C++20
The concept name must appear directly before
auto
.#include <concepts>
using std::regular;
// type of 'a' must be "regular"
regular auto a = f();
regular auto& b = f();
regular auto&& c = f();
East const
regular auto const v = f();
regular auto const& w = f();
regular auto const* x = f();
regular auto *const y = f();
regular auto const*const z = f();
const
West
const regular auto v = f();
const regular auto& w = f();
const regular auto* x = f();
regular auto *const y = f();
const regular auto *const z = f();
Class Member Functions
No difference between east and west const
,
but consistent with east const
.
// only callable on const
values
void MyClass::foo() const;
// only callable on const
lvalues C++11
void MyClass::bar() const&;
Comments…