3 ms·
look 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...
by enqk 5y ago
look 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.