5 ms·
Hey, you write `const` on the right too! There must be, like, dozens of us! Is there a style guide recommending this, or have I just managed to dodge every exa
by Twisol 5y ago
Hey, you write `const` on the right too! There must be, like, dozens of us!
Is there a style guide recommending this, or have I just managed to dodge every example that puts it on the right?
- Vogtinator 5y agoint * const foo = baz; and int const * bar = baz; have different meaning. foo is a constant pointer (you can't change the address) while bar is a pointer to a constant int (you can't change the value it's pointing at). However, int const * bar; is the same as const int * bar;
- tialaramex 5y agoIn C++ this is called East Const vs West Const with, as someone explains below, East Const (const on the right) being the only one that actually does something consistent and explicable.
- Twisol 5y agoThanks for the nomenclature! That definitely helps find more discussion around the two preferences, like [0]. (FWIW, I'm aware of the semantic differences; I've just never come across that much material referencing it, much less acknowledging the schism.) [0] https://hackingcpp.com/cpp/design/east_vs_west_const.html https://hackingcpp.com/cpp/design/east_vs_west_const.html
- enqk 5y agolook up eastside const https://mariusbancila.ro/blog/2018/11/23/join-the-east-const-revolution/ https://mariusbancila.ro/blog/2018/11/23/join-the-east-const... I’ve been using this style at home in a longtime now, I appreciate how it’s more at home with C and more consistent
- mananaysiempre 5y agoIt may look more consistent, but it misleads the reader about the (inconsistent) way the C declaration syntax works: in int *const p, *q; p is constant but q is not, while in either of const int i, j; int const i, j; both i and j are constant, and int i, const j; is a syntax error. (This is still true but less relevant in C++, which made such an unintuitive mess of the declaration syntax that now the usual advice is to avoid declaring more than one variable per declaration at all.)
- tialaramex 5y agoI don't see an inconsistency? The whole point of East Const is that the type to its left is constant. Clearly the pointer p is const and the pointer q isn't, because there's no way that asterisk next to q is affected by a const over on the far side of a comma.
- mananaysiempre 5y agoHm. I can see where you’re coming from, but I don’t think pretending the C declaration syntax is postfix or even involves a syntactic entity worthy of being called a “type” is particularly helpful (abstract declarators anyone? now Standard ML does in fact have a mostly-postfix type sublanguage). For example, what is the “type to the left” that the const in the constant function pointer declaration int *(*const f)(void); is supposed to apply to?
- tialaramex 5y agoMaybe you have higher standards for what constitutes a "type" than were prevalent in the 1970s. Obviously C isn't trying to be SML here but those are definitely types. The type of f is: constant pointer to a function that takes no parameters but returns a pointer to an integer. The thing to the const keyword's left (thus, the thing we know is constant because East Const is consistent) is an asterisk, indicating a pointer type, inside parentheses, a function, so that's a function pointer, and we can use the rules for figuring out the function declaration to determine what type of function it is. Reading types this way has to be how you do it, all the time in C, const isn't in fact a weird exception, except in the sense that obviously constant ought to be the default, but it's too late for that in C.
- SAI_Peregrinus 5y agoJens Gustett's book [Modern C](https://modernc.gforge.inria.fr https://modernc.gforge.inria.fr) recommends this. I follow it as well.