开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜