template ctor overload Segmentation fault

Tejas notrealemail at gmail.com
Mon Dec 13 09:45:35 UTC 2021


On Sunday, 12 December 2021 at 19:17:53 UTC, vit wrote:
> On Sunday, 12 December 2021 at 18:32:28 UTC, Imperatorn wrote:
>> On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote:
>>> Hello, why does this code fail to compile?
>>>
>>> ```d
>>> struct Foo(T){
>>>     this(Rhs, this This)(scope Rhs rhs){
>>>     }
>>>
>>>     this(ref scope typeof(this) rhs){
>>>     }
>>> }
>>>
>>>
>>> struct Bar{
>>> 	Foo!int foo;
>>> }
>>>
>>> void main(){
>>> }
>>> ```
>>>
>>> error: Segmentation fault (core dumped)
>>
>> What are you trying to accomplish?
>
> Something like this:
>
> ```d
> import std.traits : CopyConstness;
>
> struct UniquePtr(T){
>     alias Type = T;
>
>     this(Rhs, this This)(scope Rhs rhs)
>     if(is(CopyConstness!(Rhs, Rhs.Type*) : CopyConstness!(This, 
> This.Type*))){
>         //...
>     }
>
>     //one of copy ctors:
>     this(ref scope typeof(this) rhs){
>         //...
>     }
>
>     static UniquePtr make(Args...)(Args args){
>     	return UniquePtr.init;
>     }
> }
>
>
> void main(){
>     const UniquePtr!(int) cui = UniquePtr!(const int).make(1);
>     const UniquePtr!(const int) cuci = UniquePtr!(const 
> int).make(1);
>     UniquePtr!(const int) uci = UniquePtr!(int).make(1);
>     UniquePtr!(int) ui = UniquePtr!(int).make(1);
>
>     const UniquePtr!(int) xcui = UniquePtr!(immutable 
> int).make(1);
>     const UniquePtr!(const int) xcuci = UniquePtr!(immutable 
> int).make(1);
>
> }
> ```
>
> This work but UniquePtr canno't be inside struct because 
> Segmentation fault.

This made your previous snippet work:

```d
struct Foo(T){
     this(Rhs, this This)(scope Rhs rhs){
     }

     this(scope Foo!(T)* rhs){
     }
}


struct Bar{
	Foo!int foo;
}

void main(){
	import std.stdio:writeln;
	
	Bar bar = Bar();
	auto BAR = new Bar();
	writeln(bar, "\t", BAR, "\t", *BAR);
}
```

Definitely something funky going on behind the scenes.
I also think you should post a bug, like the above user said.


More information about the Digitalmars-d-learn mailing list