const overload

Jonathan M Davis jmdavisProg at gmx.com
Fri Sep 23 13:44:41 PDT 2011


On Friday, September 23, 2011 23:35:59 so wrote:
> On Fri, 23 Sep 2011 23:27:02 +0300, Jonathan M Davis <jmdavisProg at gmx.com>
> 
> wrote:
> > On Friday, September 23, 2011 23:19:15 so wrote:
> >> Hello everyone.
> >> 
> >> I asked this a few times with no response.
> >> Could anyone explain me what is the rational behind this?
> >> Why it won't distinguish mutable overload from immutable as in C++?
> > 
> > That compiles fine with the lastest dmd from git. Is it not compiling
> > with the
> > latest release (dmd 2.055)?
> > 
> > - Jonathan M Davis
> 
> It compiles fine but the result troubling, wouldn't you expect:
> 
> fun
> fun const
> 
> as a result? This is how it works in C++.

It uses the const version if the struct or class is const. And in neither case 
in your program is it const. It's mutable in both, so the mutable overload is 
the one that gets called in both places. Why would the const version get 
called? How would it know to call that one instead of the mutable one? I don't 
know how it could work any other way. I would have thought that it would be 
exactly the same in C++, but I don't overload on constness very often in 
either language, so I'm not necessarily familiar with all of the ins and outs 
of why C++ picks const over mutable in such cases.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list