-preview=in might break code
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Mon Oct 5 02:53:52 UTC 2020
On 10/4/20 10:08 PM, Walter Bright wrote:
> On 10/3/2020 7:49 AM, Steven Schveighoffer wrote:
>> `in ref` is a reference, and it's OK if we make this not a reference
>> in practice, because it's const.
>
> No, it is not. Because a `const ref` can be changed by another mutable
> reference to the same memory object. This is defined behavior.
>
> This suggestion turns defined behavior into implementation-defined
> behavior, meaning it will break existing code written in good faith in
> unpredictable, unreliable ways.
I will add that C++'s std::min and std::max have had this perennial
problem that still bites users. Check
https://en.cppreference.com/w/cpp/algorithm/min:
"Warning
Capturing the result of std::min by reference produces a dangling
reference if one of the parameters is a temporary and that parameter is
returned:
int n = 1;
const int& r = std::min(n-1, n+1);
// r is dangling
"
C++ compilers have gotten increasingly adept at detecting and warning
about such situations, but voluntarily adding a problematic feature to
the D language to then work on fixing its aftermath doesn't seem a wise
thing to do.
More information about the Digitalmars-d
mailing list