Deliberatly trigger a compilation error in Visual Studio 2008
Is there a way to deliberatly trigger a compilation error when a cert开发者_如何学Pythonain condition is satisfied on Visual Studio 2008?
You can use the #error pre-processor feature.
You use it like such:
#ifdef WIN32
... code for windows
#else
#error only windows is supported
#endif
In C++ you can use macros like the following to cause a compile time assert when a specified constant condition is false
#define COMPILE_ASSERT(expr) extern int __assertutil[(expr) != 0]
COMPILE_ASSERT(42 != 8); // Fine
COMPILE_ASSERT(42 == 8); // Error
This works because in the case that the expression is false it will have a constant value of 0. Arrays in C++ can not have a constant size of 0 and it leads to a compilation error.
how about the compiler directive #error?
There are language specific pre-processor directives. In C#:
#define DEBUG class MainClass {
static void Main()
{
#if DEBUG
#error DEBUG is defined
#endif
}
}
From MSDN.
int i = (float)(5.0);
I wonder if you're looking for a way to validate the source code before compilation. If so, you could use a pre-build step and fail it (exit with a code other than 0) to abort compilation.
精彩评论