struct constructor with rvalue param of same struct type

vit vit at vit.vit
Tue Jan 19 09:56:35 UTC 2021


On Tuesday, 19 January 2021 at 06:49:06 UTC, Kyle Ingraham wrote:
> I’m sorry that this isn’t a solution to your problem but your 
> code caught my attention. What is your snippet supposed to do?

This is more specific example:
https://run.dlang.io/is/W7rd2u

import std.traits : CopyTypeQualifiers;

struct Ptr(T)
if(is(T == struct)){
     private void* ptr;

     static if(is(T == const))
     this(ref const typeof(this) x)@trusted{
     	this.ptr = cast(typeof(this.ptr))x.ptr;
     }

     this(ref typeof(this) x)@safe{
     	this.ptr = x.ptr;
     }

     this(ref const typeof(this) x)const @safe{
     	this.ptr = x.ptr;
     }

     this(P : const Ptr!(U), U, this This)(auto ref P x)@trusted
     if(true
        	//&& !is(R == T)
         && is(CopyTypeQualifiers!(P, U)* : 
CopyTypeQualifiers!(This, T)*)
     ){
     	this.ptr = cast(typeof(this.ptr))x.ptr;
     }

}


struct Foo{
     //@disable this(this);
}

T get_rvalue(T)(){		
	return T.init;
}

void main()@safe{

     ///normal pointers
     {
         const(const(Foo)*) cpcf;	///const(Foo*)

         const(Foo*) cpf1 = cpcf;			        ///OK	
         const(Foo*) cpf2 = get_rvalue!(const(const(Foo)*));	///OK	

         const(Foo)* pcf1 = cpcf;				///OK	
         const(Foo)* pcf2 = const(const(Foo)*).init;		///OK
         const(Foo)* pcf3 = get_rvalue!(const(const(Foo)*));	///OK

     }

     //wrapped ptr:
     {
         const Ptr!(const Foo) cpcf;

         const Ptr!(Foo) cpf1 = cpcf;					///OK
         const Ptr!(Foo) cpf2 =  get_rvalue!(const Ptr!(const 
Foo));	///OK

         Ptr!(const Foo) pcf1 = cpcf;					///OK
         Ptr!(const Foo) pcf2 = const(Ptr!(const Foo)).init;		///OK
         Ptr!(const Foo) pcf3 = get_rvalue!(const Ptr!(const 
Foo));	///ERROR!
     }
}


More information about the Digitalmars-d-learn mailing list