I can has @nogc and throw Exceptions?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 13 11:09:59 PST 2015


On Friday, 13 February 2015 at 19:03:10 UTC, Jonathan Marler 
wrote:
> T construct(T,A...)(void* buffer, A args)
> {
>   return (cast(T)buffer).__ctor(args);
> }

This is wrong, you need to initialize the memory first to the 
proper values for the class, gotten via typeid(T).init. 
std.conv.emplace does this correctly, either use it or look at 
its source to see how to do it.

>   ubyte[ __traits(classInstanceSize, Exception)] 
> exceptionBuffer;

When the stack unwinds, this will be invalidated... I don't think 
stack allocated exceptions are ever a good idea. I don't think 
malloc exceptions are a good idea either, the catcher would  need 
to know to free it.

You might preallocate a pool of GC'd exceptions though, then 
throw the next one in the list instead of making a new one each 
time.


More information about the Digitalmars-d-learn mailing list