How about some __initialize magic?

vit vit at vit.vit
Tue Jan 4 19:39:42 UTC 2022


On Saturday, 27 November 2021 at 21:56:05 UTC, Stanislav Blinov 
wrote:
> ...

Nice idea, placement new really missing from D.

I have reverse problem with emplace then you.
emplaceRef wrongly infers @safe attribute for non ctfe code if 
assignment is @system because of this:

```d
//emplaceRef:
if (__ctfe)
     ///...
     chunk = forward!(args[0]);
     ///...
}
```

Example:
```d
struct Foo{

     this(scope ref const typeof(this) rhs)@safe{}

     void opAssign(scope ref const typeof(this) rhs)@system{}
}


void main()@safe{
     import core.lifetime : emplace;

     Foo foo;
     {
         const Foo* ptr;
         emplace(ptr, foo);	//OK __ctfe path doesn't exists
     }
     {
         Foo* ptr;
         emplace(ptr, foo);	//ERROR __ctfe path exists and call 
@system opAssign
     }
}
```
Error: `@safe` function `D main` cannot call `@system` function 
`core.lifetime.emplace!(Foo, Foo).emplace`


D has one good thing, you can create custom emplace which run 
your own code between emplaceInitialize and ctor. You can 
initialize your own vptr before ctor.



More information about the Digitalmars-d mailing list