Why do we have transitive const, again?
Jonathan M Davis
jmdavisProg at gmx.com
Fri Sep 23 14:55:10 PDT 2011
On Friday, September 23, 2011 14:37 Mehrdad wrote:
> On 9/23/2011 11:47 AM, Jonathan M Davis wrote:
> > ...You'd have to duplicate functions all over the place... - Jonathan
> > M Davis
>
> Can't you avoid that trivially with templates?
Then all of your functions have to be templated. Templated functions can't be
virtual. And even if they _could_ be, that still requires that programmers
template or duplicate functions all over the place. It not only increases the
amount of bloat in the binary, but it creates more work for the programmer.
Honestly, I don't understand what the problem with transitive const is. I see
potential issues with the fact that it's not guaranteed to work to cast away
const and alter the variable that was const. The lack of anything even similar
to logical const _can_ be annoying. But the transitiveness of const? I really
don't see any issue. It makes it easier for the compiler to make strong
guarantees about type safety (especially when joined with the fact that
casting away const and then mutating is undefined behavior), and it closes
what I would consider enormous holes in C++'s const system. I would think that
the fact that if you pass a variable to function which takes its arguments as
const can _guarantee_ that that variable hasn't been altered after the call
(assuming that there are no other references to it - or if the function is
pure, assuming that you don't pass any other non-const references to that data
to it) would be _very_ desirable. C++ can't guarantee anything of the sort,
and if const in D weren't transitive, it couldn't make such guarantees either.
What are you trying to do that the transitiveness of const causes issues? I
just don't see the problem.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list