这是静态断言技巧的一部分。我无法理解非专业班是如何工作的。有人可以向我解释吗?这段代码是什么意思?
编辑:与宏全码:(从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