我不是指編譯錯誤,因爲我犯了一個語法錯誤或任何其他錯誤。在C++中,我們可以創建編譯時錯誤基礎上的條件,如下面的例子:在Java中產生條件編譯時錯誤
template<int> struct CompileTimeError;
template<> struct CompileTimeError<true> {};
#define STATIC_CHECK(expr, msg) { CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }
int main(int argc, char* argv[])
{
STATIC_CHECK(false, Compile_Time_Failure);
return 0;
}
在VS 2005這將輸出:
------ Build started: Project: Test, Configuration: Debug Win32 ------
Compiling...
Test.cpp
f:\temp\test\test\test.cpp(17) : error C2079: 'ERROR_Compile_Time_Failure' uses undefined struct 'CompileTimeError<__formal>'
with
[
__formal=0
]
Build log was saved at "file://f:\temp\Test\Test\Debug\BuildLog.htm"
Test - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
有沒有辦法在Java中實現這一目標?