书中有个例子如下:(名称有些不同)
template<bool>
class Check
{
public:
Check(...);
};
template <> class Check<false>{};
#define STATIC_CHECK(exp, msg) \
{\
class ERROR##msg {};\
(void)sizeof(Check<(exp)>(ERROR##msg()));\
}
使用VC7和最新的gcc都无法编译的过,难道是这个标准还没有支持?
所以尝试多次后改为:
template<bool>
class Check
{
public:
Check(...);
};
template <> class Check<false>{};
#define STATIC_CHECK(exp, msg) \
{\
class ERROR##msg {};\
ERROR##msg e;\
Check<(exp)> aCh(e);\
}
这里就不用使用sizeof了
template <bool x> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 };
};
#define STATIC_ASSERT( B ) \
typedef static_assert_test < sizeof(STATIC_ASSERTION_FAILURE<( B )>)
> \
ERROR_ASSERT
namespace Loki
{
template<int> struct CompileTimeError;
template<> struct CompileTimeError<true> {};
}
#define LOKI_STATIC_CHECK(expr, msg) \
{ Loki::CompileTimeError<((expr) != 0)> ERROR_##msg;
(void)ERROR_##msg; }
因为编译器认为:
Check<(exp)>();
//这个不是构造一个类的实例,而是认为Check是个template
function,而且显式调用,其参数类型为exp。
所以编译不过。