How to convert member function to free function?
H. S. Teoh
hsteoh at quickfur.ath.cx
Fri Sep 18 18:43:38 UTC 2020
On Fri, Sep 18, 2020 at 06:20:41PM +0000, Andrey Zherikov via Digitalmars-d-learn wrote:
> How can I rewrite foo() function as a free-function that won't cause
> struct copying? My simplified code is:
> ========
> struct S
> {
> ref S foo() return
> {
> return this;
> }
> }
>
> void main()
> {
> S().foo().foo().foo();
> }
> ========
> If I write it like "auto foo(S s) { return s; }" then statement in
> main() will copy value of S three times and I want to avoid this.
Why can't you return by ref, which would also avoid the copying?
ref S foo(return ref S s) { return s; }
T
--
Let's not fight disease by killing the patient. -- Sean 'Shaleh' Perry
More information about the Digitalmars-d-learn
mailing list