Problems with D const ref vs C++ const &

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Dec 10 12:29:55 PST 2010


On 12/10/10 9:48 AM, Craig Black wrote:
> In C++ I could to the following:
>
> Vector3 cross(const Vector3 &a, const Vector3 &b) { ... }
>
> and then call it like this:
>
> Vector3 a, b, c;
> a = cross(b, c);
> a = cross(b-a, c-a);
>
> But in D I have to define two functions if I want pass by reference to
> work:
>
> Vector3 cross(ref const Vector3 a, ref const Vector3 b) {...}
> Vector3 cross(Vector3 a, Vector3 b) {...}
>
> Not having an l-value forces me to pass by value, and I have to define
> two functions. I know about auto ref but that only works for templates,
> and the compiler generates two functions which could lead to code bloat
> if the function is a big one.
>
> -Craig
>

auto ref should work for non-templates and generate only one function. 
The current implementation of auto ref originates in a misunderstanding.


Andrei


More information about the Digitalmars-d mailing list