How to pass noncopyable variadic arguments with ref?

ryuukk_ ryuukk.dev at gmail.com
Fri Oct 21 12:05:28 UTC 2022


On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:
> Hi,
> I've found strange behavior where:
>
> ```D
> import std.stdio;
>
> struct Foo
> {
>     @disable this(this);
>     int x;
> }
>
> void test(Foo[] foos...)
> {
>     foreach (ref f; foos) {
>         writeln(&f, ": ", f.x);
>         f.x = 0;
>     }
> }
>
> void main()
> {
>     Foo f1 = Foo(1);
>     Foo f2 = Foo(2);
>     writeln("f1: ", &f1);
>     writeln("f2: ", &f2);
>     test(f1, f2);
>     writeln("f1: ", f1.x);
>     writeln("f2: ", f2.x);
> }
> ```
>
> Compiles fine (no error on passing noncopyable arguments to the 
> function), but there are other objects passed to the function 
> as they aren't cleared out in the caller scope.
>
> Shouldn't it at least protest that objects can't be passed to 
> the function as they aren't copyable?


     void test(Foo..)(Foo foos)

I don't know if that's the 1:1 alternative, but that doesn't 
compile

     onlineapp.d(23): Error: struct `onlineapp.Foo` is not 
copyable because it has a disabled postblit


More information about the Digitalmars-d-learn mailing list