82、静态 assertion
C++ 提供了两种⽅式来 assert :⼀种是 assert 宏,另⼀种是预处理指令 #error。前者在 运⾏期起作⽤,而后者是预处理期起作⽤。它们对模板都不好使,因为模板是编译期的概念。 static_assert 关键字的使⽤方式如下:
template< class T >
struct Check {
static_assert( sizeof(int) <= sizeof(T), "T is not big enough!" ) ;
} ;Last updated