Note: I have deleted my previous version of this question as I found it lacking crucial information and therefore being prone to misunderstandings.

Question : In C/C++ you can position the keyword 'const' either left or right of the left-most type specifier. Which variant do you prefer?

I ask that because I'd like to hear your opinion. Although I have been working with C over three decades now, I only learned this a couple of years ago. After some experimentation I decided for myself, that I like the placement to the right more. Although the positioning to the left is taught in literally every book and course, the original placement suits me better.

One reason, of many, is the listing of many member variables in structs or classes. To have them nicely aligned, I always had to put 'const' either on the previous line or put in extra indention to everything non-const. That was quite irritating sometimes.

Another, and my main reason is, that when reading from right to left, the rhs variant just makes more sense than the lhs variant. Reading from left to right almost never makes much sense without straining your eyes. But that is, of course, highly subjective.

This is even more so if you have pointers. The 'const' keyword modifies the type identifier(s) to the left. So if the 'const' is (anywhere) left of the '*', the data is const. If the 'const' is right of the '*', the pointer address itself is const. The same applies to references.

Examples, read right-to-left:
int* const i; // i is a const pointer to int data
int const* i; // i is a pointer to const int data
int const* const obj; // i is a const pointer to const int data

The "classical" or "taught" way, that is found almost everywhere would read, still right-to-left:
int* const i; // i is a const pointer to int data
const int* i; // i is a pointer to int data const
const int* const obj; // i is a const pointer to int data const

Not only that the second "lhs" form reads worse, it also looks worse. In my opinion, the first "rhs" variant makes it simpler to quickly determine that we are dealing with three ints, while on the second "lhs" variant, one has to first get past the 'const' keywords.

I know that this is not only a matter of taste, but of course of agreement, too. You can not just go and switch the 'const' placement in long standing projects. That would surely piss of a lot of people. Or even cost you your job.
But I like to know what you people think and why.

Thanks a lot in advance!

  • 1
    I always put const before the type. Just how I like to do it. I mostly write Typescript at the moment so the const keyword is always first.
  • 1
    constant static char* a;
  • 0
    @olback @netikras thank you for your feedback!
  • 2
    I prefer const on LHS, because I put a lot of emphasis on whether something is const or not, mutable refs/pointers really scare me because they can (and have :/) lead to so many subtle bugs. I also believe that's the intention of most books having const on LHS, but I could be wrong there. Basically, if something is mutable I want that fact to jump out immediately. I'm also a big fan of the immutable-first type of programming you see in functional languages like OCaml, so yeah.

    I get pretty mental with const, lol, I put as much of it as possible in my code. It's magic.
  • 1
    @RememberMe you certainly have a point here.

    I work with as much cost as possible, too, so I concur. Makes everything safer and subtle bugs to be easier to spot. Well, no magic here when the compiler yells at you... 😂

    Well, I also like my sources to be nice to read, so I put much effort into aligning nicely. I just find it easier on the eyes. But putting const on the previous line always looks so disruptive.
Add Comment