Overload resolution (value vs reference)

Jonathan M Davis jmdavisProg at gmx.com
Mon Oct 22 14:57:51 PDT 2012


On Monday, October 22, 2012 22:54:44 Era Scarecrow wrote:
> On Sunday, 21 October 2012 at 22:47:00 UTC, Jonathan M Davis
> 
> wrote:
> > http://dlang.org/function.html#function-overloading
> > 
> > The big thing to remember here is that constness matters more
> > than refness when overloads are chosen, so if you want refness
> > to matter when choosing an overload, then the constness of the
> > overloads must match, and if there's ever a question between
> > const and non-const, it's the constness of the argument being
> > passed in which wins (e.g. if you have ref T and const ref T,
> > then which one gets called depends on whether the argument is
> > const or not).
> 
> There any word on if this was going to change? I'm meaning
> towards structs/classes with const preference vs non-const
> preferences. I recall a while back while trying to make copy/move
> functions and I had to completely duplicate the functions so it
> would use the right versions.

AFAIK, there are no plans to change it. I don't think that it's even been 
suggested.

For the most part though, you can just have the non-ref version call the ref 
version as long as there's a ref version with the same constness, so even if 
you have to duplicate the function, you don't have to duplicate its body. You 
might be stuck with some duplication between the const and non-const overloads 
though if you have all 4 of them.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list