error: rvalue constructor and a copy constructor

vit vit at vit.vit
Wed Jan 12 08:04:19 UTC 2022


Hello, I have this code:

```d

import core.lifetime : move, forward;
import std.stdio : writeln;

struct Foo{
     int i;

     static auto make(int i){
     	Foo tmp;
         tmp.i = i;
         return move(tmp);
     }

     this(scope const ref typeof(this) rhs){
     	this.i = rhs.i;
         writeln("copy");
     }

     this(T)(scope const T rhs)
     if(is(immutable T == immutable typeof(this))){
     	this.i = rhs.i;
         writeln("move");
     }

}

auto foo(T)(auto ref T x){
	return Foo(forward!x);
}

void main(){
     Foo x = foo(Foo.make(1));
     writeln("end");
}
```

When is this code compiled with 2.098.1 then it print:

```
Error: Cannot define both an rvalue constructor and a copy 
constructor for `struct Foo`
        Template instance `__ctor!(Foo)` creates a rvalue 
constructor for `struct Foo`
Error: template instance `onlineapp.Foo.__ctor!(Foo)` error 
instantiating
        instantiated from here: `foo!(Foo)`
```

When is compiled by dmd-beta/dmd-nightly then it works fine.

Is dmd-beta/dmd-nightly with bug or is this new valid behaviour?


More information about the Digitalmars-d-learn mailing list