p0nce / d-idioms

"Effective D" without the haircut.
http://p0nce.github.io/d-idioms/
82 stars 17 forks source link

@disable new trick #141

Open p0nce opened 6 years ago

p0nce commented 6 years ago

By Basile, co-invented with Ketmar and Adam


/**
 * When mixed in a aggregate this template has for effect to disable the usage
 * the $(D new) operator.
 */
mixin template disableNew()
{
    @disable new (size_t){return null;}
}
///
unittest
{
    // class requiring users to use allocators.
    class NotUsableWithNew
    {
        mixin disableNew;
    }

    // statically verify that `new` cannot be used.
    static assert(!__traits(compiles, new NotUsableWithNew));
}