How to pass noncopyable variadic arguments with ref?
Imperatorn
johan_forsberg_86 at hotmail.com
Thu Oct 20 16:17:25 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?
Have you looked at the ast?
More information about the Digitalmars-d-learn
mailing list