DIP 1016 and const ref parameters
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Wed Jun 19 12:55:09 UTC 2019
On Wednesday, June 19, 2019 6:33:44 AM MDT XavierAP via Digitalmars-d-learn
wrote:
> I often use a pattern of having const ref struct parameters (as
> in C++) but this doesn't work in the case of rvalues. The
> workaround of defining an overload that calls its own name is
> terrible. I understand there was a DIP 1016 by Manu asking for
> this case to work. As far as I can tell, this was rejected, but
> later reconsidered, and now Andrei is starting up a new one[1]?
> Apologies but I'm not sure where these discussions are
> centralized. But if anyone has any idea or guess how seriously
> and in what kind of time this could be expected, that would be my
> first side question.
>
> My main learning question is whether the const ref parameter
> pattern is good in D? In C++ I see it everywhere, but are there
> better alternatives, in particular in D, or is there no point
> because some copy elision optimization may be guaranteed? In
> short am I right in writing const ref parameters, or am I doing
> something silly (and as important as this DIP may otherwise be,
> it wouldn't affect me as much as I think)??
>
> As far as I can see, this DIP would be helpful for two use cases:
> const ref, and return ref with method chains. Are there others?
> __________
> [1]
> https://forum.dlang.org/post/d90a7424-a986-66f1-e889-a9abd55e0e65@erdani.o
> rg
Even in C++, using const ref is not as good a practice as it once was,
because they added move constructors, finally making object moveable. The
result is that in many cases, it's actually more efficient to just copy
values in C++ rather than use const &, but which is better does depend on
the code.
As for D, unless you're dealing with large objects, odds are that worrying
about passing by value is pointless. D classes are reference types, and D
structs have move semantics built-in. So, you don't get as many copies as
you would in C++98, and the situation is probably better than newer versions
of C++, since IIRC, C++ classes aren't moveable by default, whereas D
structs are. In general, you're probably better off just passing by value
unless you find that a particular piece of code is inefficient when
benchmarking. Either way, you don't want to be slapping const on everything
the way you would in C++, because D's const is far more restrictive. So,
while it still can be quite useful, odds are that if you start using it
heavily, you're going to run into problems fast - especially since casting
away const and mutating an object is undefined behavior in D. D's const has
no back doors. If something is const, then you can't mutate it unless you
also have a mutable reference to the same data. And because const is
transitive, you pretty much can't get mutable stuff from const stuff like
you frequently can in C++ (e.g. in C++, it's possible to have a const
container of mutable objects, wherein D, once part of something is const,
everything within that part is const).
As for the DIP, I'd suggest watching Andrei's recent dconf talk on the
subject:
https://www.youtube.com/watch?v=aRvu2JGGn6E&feature=youtu.be
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list