On exceptions in D
Dmitry Olshansky
dmitry.olsh at gmail.com
Sun Feb 9 12:32:43 PST 2014
09-Feb-2014 22:14, Jakob Ovrum пишет:
> On Sunday, 9 February 2014 at 17:57:23 UTC, Dmitry Olshansky wrote:
>> void risef(T...)(string fmt, T args)
>> {
>> static Failure!T slot;
>> if(!slot)
>> slot = new Failure!T();
>> slot.assign(fmt, args);
>> throw slot;
>> }
>
> If the template is instantiated we can say with almost certainty that
> the function will be called, so it'd be an improvement to allocate the
> exception instance statically (lazily initialized with emplace), instead
> of statically allocating just a reference to a GC-allocated instance.
Might be a good idea but compiler is pretty conservative with what can
be created at compile-time and emplace may not play nice with CTFE.
> Also, it's nice to keep the throw statement in user code, e.g. `throw
> allocExf(fmt, args);`, as `throw` is a no-return statement like
> `return`, `assert(false)` etc.
Good idea. Then call it 'exception(f)' and use like this:
throw exceptionf("Message with some %s substitution", "writef-like");
--
Dmitry Olshansky
More information about the Digitalmars-d
mailing list