static_assert
是C++11中引入的一个关键字,用于在编译期间进行断言。它的语法很简单:static_assert(常量表达式,提示字符串)
。如果第一个参数常量表达式的值为真(true或者非零值),那么static_assert
不做任何事情,就像它不存在一样,否则会产生一条编译错误,错误位置就是该static_assert
语句所在行,错误提示就是第二个参数提示字符串¹。
static_assert
可以用在全局作用域中,命名空间中,类作用域中,函数作用域中,几乎可以不受限制的使用。编译器在遇到一个static_assert
语句时,通常立刻将其第一个参数作为常量表达式进行演算,但如果该常量表达式依赖于某些模板参数,则延迟到模板实例化时再进行演算。
这是一个简单的例子:
static_assert (sizeof (void *) == 4, "64-bit code generation is not supported.");
这个static_assert
用来确保编译仅在32位的平台上进行,不支持64位的平台,该语句可以放在文件的开头处,这样可以尽早检查,以节省失败情况下的编译时间。
另一个例子:
struct MyClass
{
char m_value;
};
struct MyEmptyClass
{
void func ();
};
static_assert (std::is_empty<MyEmptyClass>::value, "empty class needed");
static_assert (!std::is_empty<MyClass>::value, "non-empty class needed");
template <typename T, typename U, typename V>
class MyTemplate
{
static_assert (!std::is_empty<T>::value, "T should be a non-empty class");
static_assert (std::is_empty<U>::value, "U should be an empty class");
static_assert (std::is_base_of<std::allocator<T>, V>::value, "V should inherit from std::allocator<T>");
};
template class MyTemplate<MyClass, MyEmptyClass, std::allocator<MyClass>>;
通过这个例子我们可以看出来,static_assert
可以很灵活的使用,通过构造适当的常量表达式,我们可以检查很多东西。比如范例中std::is_empty
和std::is_base_of
都是C++新的标准库提供的type traits模板,我们使用这些模板可以检查很多类型信息。这对于嵌入式系统编程来说非常有用,因为它可以帮助我们在编译期间发现更多的错误,用编译器来强制保证一些契约,并帮助我们改善编译信息的可读性。