digitalmars.D.learn - Assert compilation failure with certain message
- Tomek =?ISO-8859-2?Q?Sowi=F1ski?= (3/3) Feb 10 2011 Is there a way to statically assert compilation of an expression failed ...
- bearophile (4/5) Feb 10 2011 I have asked something like this a lot of time ago, but I don't know a w...
- Tomek =?ISO-8859-2?Q?Sowi=F1ski?= (8/14) Feb 10 2011 y to do it. You are able to statically
- Andrej Mitrovic (19/19) Feb 10 2011 How's this?
- Andrej Mitrovic (19/19) Feb 10 2011 I've managed to screw up the colon placement though, here's a quick fix:
- Tomek =?ISO-8859-2?Q?Sowi=F1ski?= (5/28) Feb 11 2011 How does it help to find out that compilation tripped on a specific stat...
- Andrej Mitrovic (1/1) Feb 11 2011 I thought you were just looking for a static assert with a custom messag...
- bearophile (4/5) Feb 10 2011 Asking for new features in this newsgroup is not so useful. You may add ...
- Jonathan M Davis (11/14) Feb 10 2011 You mean like
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. -- Tomek
Feb 10 2011
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
Feb 10 2011
bearophile napisa=B3:*with a certain message*? I want to checkIs there a way to statically assert compilation of an expression failed=y to do it. You are able to staticallymy static asserts trip when they should. =20=20 I have asked something like this a lot of time ago, but I don't know a wa=assert that some code doesn't compile, but I don't know how to assert tha=t a certain message gets produced. You areasking for a specific static catch :-)Static catch, yeah. But I'd be content with traits__(fails, expr, msg) whic= h seems tractable. --=20 Tomek
Feb 10 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; }
Feb 10 2011
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; }
Feb 10 2011
Andrej Mitrovic napisa=B3:I've managed to screw up the colon placement though, here's a quick fix: =20 import std.stdio; import std.conv; =20 void staticAssert(alias exp, string message, string file =3D __FILE__, int line =3D __LINE__)() { static if (!exp) { pragma(msg, file ~ "(" ~ to!string(line) ~ "): " ~ "staticAssert: " ~ to!string(message)); assert(0); } } =20 void main() { enum x =3D false; staticAssert!(x, "Oh no we failed!"); =20 int y; }How does it help to find out that compilation tripped on a specific static = assertion? --=20 Tomek
Feb 11 2011
I thought you were just looking for a static assert with a custom message?
Feb 11 2011
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
Feb 10 2011
On Thursday, February 10, 2011 16:12:01 Tomek Sowi=C5=84ski 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 t= rying=20 to assert something like that a particular template instantiation fails, th= e use=20 static assert(!__traits(compiles, exp)); where exp is the expression being= =20 tested. =2D Jonathan M Davis
Feb 10 2011