www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Class Allocators

reply Kyle <kyle.kyle kyle.kyle> writes:
I've been doing some review of Andrei's book which has led me to 
trying to figure out how placement new works, or doesn't work. 
The new(address) Type syntax tells me "no allocator for TYPE". I 
did find information on "Class Allocators" at 
https://dlang.org/spec/class.html#allocators, but when I try to 
do things the Class Allocator way DMD tells me that "class 
allocators are obselete, consider moving the allocation strategy 
outside of the class". My best guess right now is that both class 
allocators and the placement new syntax are deprecated, but if 
that's the case I would expect a deprecation message when I try 
to use that new(address) Type syntax whether there's a class 
allocator present or not. Any insight into this? Thanks.
Jan 31
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Sunday, 31 January 2021 at 23:19:09 UTC, Kyle wrote:

 strategy outside of the class". My best guess right now is that 
 both class allocators and the placement new syntax are 
 deprecated, but if that's the case I would expect a deprecation 
 message when I try to use that new(address) Type syntax whether 
 there's a class allocator present or not. Any insight into 
 this? Thanks.
Class allocators were deprecated in 2.080 and became an error in 2.087: https://dlang.org/deprecate.html I assume the placement new syntax was supposed to have been deprecated as well. It's possible it was overlooked, or that it wasn't deprecated because it has an obscure use-case that I'm unaware of. These days we should be using std.conv.emplace: https://dlang.org/phobos/std_conv.html#.emplace.4 See also: http://p0nce.github.io/d-idioms/#Placement-new-with-emplace I suggest you file an issue at: https://issues.dlang.org/
Jan 31
prev sibling parent evilrat <evilrat666 gmail.com> writes:
On Sunday, 31 January 2021 at 23:19:09 UTC, Kyle wrote:
 My best guess right now is that both class allocators and the 
 placement new syntax are deprecated, but if that's the case I 
 would expect a deprecation message when I try to use that 
 new(address) Type syntax whether there's a class allocator 
 present or not. Any insight into this? Thanks.
Yes, just use emplace() insead of placement new. GC-less allocations however is up to you, either malloc/free, std.experimental.allocator or any other way. You can make your own smart pointer struct to handle this automatically, or better use community packages such as 'automem'. As for the message it is possible that this part of the reference compiler was already passed deprecation period and should be removed but was completely forgotten. https://dlang.org/phobos/core_lifetime.html#.emplace
Jan 31