Binding rvalues to ref [WAS: I close BIP27. I won't be pursuing BIPs anymore]

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed Oct 19 18:04:35 PDT 2016


On Thursday, October 20, 2016 10:23:35 Manu via Digitalmars-d wrote:
> On 20 October 2016 at 01:38, Jonathan M Davis via Digitalmars-d <
>
> digitalmars-d at puremagic.com> wrote:
> > On Wednesday, October 19, 2016 07:55:19 Andrei Alexandrescu via
> > Digitalmars-d
> >
> > wrote:
> > > This was C++'s big un' that led to many complications. If the overload
> > > weren't ambiguous, a large part of rvalue references would have been
> > > unneeded. (Universal references would still have been necessary for
> > > perfect forwarding, but that's not the bulk.)
> > >
> > > In order to avoid such issues, we steered clear off binding rvalues to
> > > ref parameters in the D language. As I mentioned to Ethan, I do agree a
> > > careful definition may be able to avoid the fallout that happened in
> > > C++. It would be a fair amount of work.
> >
> > The other big problem is that D's const is so much more restrictive than
> > C++'s that even if const ref accepted rvalues, a large portion of the
> > time,
> > it would be too restrictive to be useful.
>
> I've never seen a piece of code in C++ that receives const& that isn't
> strictly read-only.
> I can't imagine from experience how D's const would change the usefulness
> of the pattern.

The transitivity of const shoot stuff in the foot pretty thoroughly in a
number of cases. A prime example would be ranges, because they have to be
mutated to be iterated over. If the function actually took a range directly,
you wouldn't bother with const ref, but it could be an object that contains
a range, and because you can't normally get a tail-const range from a const
range (aside from dynamic arrays), it can become quite difficult to actually
iterate over the range. e.g.

auto foo(ref const(Bar) bar)
{
    auto range = bar.getSomeRange();
    ...
}

Because getSomeRange would have to be const (or inout) in order to be
called, the return type is going to have be const if there are any
indirections in it. And if it's const, and it's a range, it's useless.

If this were C++, const wouldn't be transitive, so it would be trivial to
get an iterator or range which wasn't const and did not violate const or
attempt to work around it in any way and thus could be iterated. But in D,
const is far too restrictive for that to work.

This is the kind of problem that quickly makes you start wondering whether
it's even worth trying to use const in D at all.

- Jonathan M Davis



More information about the Digitalmars-d mailing list