Re: Assert compilation failure with certain message
I thought you were just looking for a static assert with a custom message?
Re: Assert compilation failure with certain message
Andrej Mitrovic napisał: > I've managed to screw up the colon placement though, here's a quick fix: > > 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; > } How does it help to find out that compilation tripped on a specific static assertion? -- Tomek
Re: Assert compilation failure with certain message
Tomek S.: > Static catch, yeah. But I'd be content with traits__(fails, expr, msg) which > seems tractable. Asking for new features in this newsgroup is not so useful. You may add it to bugzilla... Bye, bearophile
Re: Assert compilation failure with certain message
I've managed to screw up the colon placement though, here's a quick fix: 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; }
Re: Assert compilation failure with certain message
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; }
Re: Assert compilation failure with certain message
bearophile napisał: > > Is there a way to statically assert compilation of an expression failed > > *with a certain message*? I want to check > > my static asserts trip when they should. > > I have asked something like this a lot of time ago, but I don't know a way to > do it. You are able to statically > assert that some code doesn't compile, but I don't know how to assert that a > certain message gets produced. You are > asking for a specific static catch :-) Static catch, yeah. But I'd be content with traits__(fails, expr, msg) which seems tractable. -- Tomek
Re: Assert compilation failure with certain message
On Thursday, February 10, 2011 16:12:01 Tomek Sowiński wrote: > Is there a way to statically assert compilation of an expression failed > *with a certain message*? I want to check my static asserts trip when they > should. You mean like static assert(0, "We have a failure, Captain!"); If a static assert fails, it's obvious. Compilation fails. Now, if you're trying to assert something like that a particular template instantiation fails, the use static assert(!__traits(compiles, exp)); where exp is the expression being tested. - Jonathan M Davis
Re: Assert compilation failure with certain message
Tomek Sowiñski: > Is there a way to statically assert compilation of an expression failed *with > a certain message*? I want to check my static asserts trip when they should. I have asked something like this a lot of time ago, but I don't know a way to do it. You are able to statically assert that some code doesn't compile, but I don't know how to assert that a certain message gets produced. You are asking for a specific static catch :-) Bye, bearophile