Should it compile?

Jack Applegame japplegame at gmail.com
Sun Jun 7 23:09:41 UTC 2020


On Saturday, 6 June 2020 at 11:58:06 UTC, Basile B. wrote:
> maybe it shouldn't but then with another message, for example
>
>     Error, cannot `void` initialize a `const` declaration.
>
> since that makes very little sense, at least as a local 
> variable. (as a member, this can be initialized in a 
> constructor)
The local variable is just an example.
I need to move a constant structure from one place in memory to 
another.

I want to write something like C++ std::unique_ptr.

Consider (this is not real code, but just a simple example):
https://run.dlang.io/is/mUUU8c
```
import core.memory : pureMalloc, pureFree;
import std.algorithm : move, moveEmplace;
import std.traits : hasElaborateDestructor;

struct NonCopyable {
     this(this) @disable;
}

struct Unique(T) {
     this(this) @disable;
     T* m_data;
     this(T data) {
         m_data = cast(T*) pureMalloc(T.sizeof);
     	moveEmplace(data, *m_data);
     }
     ~this() {
         if(m_data) {
         	static if(hasElaborateDestructor!T) (cast(Unqual!T*) 
m_data).__xdtor;
             pureFree(m_data);
         }
     }
}

void main() {
     NonCopyable a;
     auto ua = Unique!NonCopyable(move(a)); // fine

     const NonCopyable const_a;
     auto const_ua = Unique!(const NonCopyable)(move(ca)); // 
error, why???
}
```



More information about the Digitalmars-d-learn mailing list