Assert compilation failure with certain message

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Feb 10 16:50:12 PST 2011


How's this?

import std.stdio;
import std.conv;

void staticAssert(alias exp, string message, string file = __FILE__,
int line = __LINE__)()
{
    static if (!exp)
    {
        pragma(msg, file ~ ":(" ~ to!string(line) ~ ") " ~
"staticAssert: " ~  to!string(message));
        assert(0);
    }
}

void main()
{
    enum x = false;
    staticAssert!(x, "Oh no we failed!");

    int y;
}


More information about the Digitalmars-d-learn mailing list