www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to pass noncopyable variadic arguments with ref?

reply tchaloupka <chalucha gmail.com> writes:
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?
Oct 20 2022
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
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?
Oct 20 2022
prev sibling next sibling parent reply user1234 <user1234 12.de> writes:
On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:
 Hi,
 I've found strange behavior where:
 [...]
 Shouldn't it at least protest that objects can't be passed to 
 the function as they aren't copyable?
it's clearly a compiler bug to me. Something is not checked when the call is verified.
Oct 20 2022
parent user1234 <user1234 12.de> writes:
On Thursday, 20 October 2022 at 16:34:34 UTC, user1234 wrote:
 On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:
 Hi,
 I've found strange behavior where:
 [...]
 Shouldn't it at least protest that objects can't be passed to 
 the function as they aren't copyable?
it's clearly a compiler bug to me. Something is not checked when the call is verified.
however (forgot to say) this form of variadic was proposed for deprecation. So maybe the bug is more an argument to drop them.
Oct 20 2022
prev sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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
Oct 21 2022
parent tchaloupka <chalucha gmail.com> writes:
On Friday, 21 October 2022 at 12:05:28 UTC, ryuukk_ wrote:
 On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:

     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
Yeah, I've ended up with this kind of workaround too. Posted a bug report: https://issues.dlang.org/show_bug.cgi?id=23452
Nov 03 2022