這是靜態斷言技巧的一部分。我無法理解非專業班是如何工作的。有人可以向我解釋嗎?這段代碼是什麼意思?
編輯:與宏全碼:(從http://www.skynet.ie/~caolan/Fragments/C++StaticAssert.html拍攝)
#ifndef STATICASSERT_HXX
#define STATICASSERT_HXX
/*
Lifted direct from:
Modern C++ Design: Generic Programming and Design Patterns Applied
Section 2.1
by Andrei Alexandrescu
*/
namespace ww
{
template<bool> class compile_time_check
{
public:
compile_time_check(...) {}
};
template<> class compile_time_check<false>
{
};
}
/*
Similiar to assert, StaticAssert is only in operation when NDEBUG is not
defined. It will test its first argument at compile time and on failure
report the error message of the second argument, which must be a valid c++
classname. i.e. no spaces, punctuation or reserved keywords.
*/
#ifndef NDEBUG
# define StaticAssert(test, errormsg) \
do { \
struct ERROR_##errormsg {}; \
typedef ww::compile_time_check< (test) != 0 > tmplimpl; \
tmplimpl aTemp = tmplimpl(ERROR_##errormsg()); \
sizeof(aTemp); \
} while (0)
#else
# define StaticAssert(test, errormsg) \
do {} while (0)
#endif
#endif
@Dan:看我的編輯。爲什麼宏有一個`sizeof(aTemp);`行? – nakiya 2011-01-28 06:11:54
@Dan實際上`compile_time_check :: compile_time_check()`會編譯,因爲默認的構造函數可用。這就是爲什麼具有looong名稱的結構正在被傳遞。 – ssmir 2011-01-28 06:16:06