Why I use "SomeObj const&", not "const SomeObj&"

This is a minor point of style, but it's important to me.

For some reason, the widely accepted C++ style is to put const in front of things:

void Function(const std::vector<int>& v)
    const int i = 123;

I pronounce this a bad choice. There's another legal way to use const, and I prefer it:

void Function(std::vector<int> const& v)
    int const i = 123;

This is why:

const void* const* const p = ...;    // Not so clear and consistent
void const* const* const p = ...;    // Clear and consistent

void Method() const;                 // const comes after Method

When a pointer is const, the keyword comes after the asterisk. When a method is const, the keyword comes after the method. When a type is const, putting const after the type is always consistent.

Putting const in front, as per the usual style, makes things like:

const void* const* const p = ...;

... kinda confusing.


Popular posts from this blog

"Unreachable" beauty standards

Is the internet ready for DMARC with p=reject?

When monospace fonts aren't: The Unicode character width nightmare