News for hackers. Stuff that matters.
Re:Const and Optimization in C by btrask on Monday July 25, 2016 @04:55AM (#12157337)
What is the argument in favor of specifically allowing casting away const of pointers?
Re:Const and Optimization in C by ridiculous_fish on Monday July 25, 2016 @05:13AM (#12157400)
The strongest argument is to support a sort of poor man's const-generics. Consider a function like strchr(). This locates a character in a (const) string, and returns a (non-const) pointer to it.

The idea is that you can use this on both const and non-const strings. Call it on a const string, you get back a non-const pointer (which you better treat as const!) But call it on a non-const string, you get back a non-const pointer, with which you can mutate the string. So a single function serves both const and non-const uses.

If casting away const-ness were disallowed, the compiler might conclude that strchr()'s returned value cannot alias the input string, and its optimizations would defeat this design. Anyways that's the original rationale.

Re:Const and Optimization in C by mannykannot on Monday July 25, 2016 @08:05PM (#12162456)
It does not strike me as much of a benefit, and certainly not worth the cost of creating confusing semantics (in fact, I would prefer (a true) const to be the default in any language, with something like 'mutable' required to override.)
Re:Const and Optimization in C by nialv7 on Tuesday July 26, 2016 @09:54PM (#12170005)
And that's why D has the 'inout' qualifier
Re:Const and Optimization in C by Rexxar on Monday July 25, 2016 @05:26AM (#12157452)
- In some case implementing both "const" and "non const" methods cause a lot of code duplications and cast can help.

- Compatibility with old libraries

Re:Const and Optimization in C by WalterBright on Monday July 25, 2016 @02:21PM (#12160499)
> implementing both "const" and "non const" methods cause a lot of code duplications

D has an `inout` qualifier that has the effect of "transmitting" the const-ness of an argument to the return type.

Re:Const and Optimization in C by WalterBright on Monday July 25, 2016 @05:17AM (#12157411)
Logical const and expressiveness.