• static_assert


    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.");
    
    • 1

    这个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");
    };
    
    template class MyTemplate<MyClass, MyEmptyClass, std::allocator<MyClass>>;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    通过这个例子我们可以看出来,static_assert可以很灵活的使用,通过构造适当的常量表达式,我们可以检查很多东西。比如范例中std::is_emptystd::is_base_of都是C++新的标准库提供的type traits模板,我们使用这些模板可以检查很多类型信息。这对于嵌入式系统编程来说非常有用,因为它可以帮助我们在编译期间发现更多的错误,用编译器来强制保证一些契约,并帮助我们改善编译信息的可读性。

  • 相关阅读:
    python r代表什么意思
    Vue----全局组件和局部组件
    HTML5+css3课后习题【二】
    Spring 从入门到精通 (九) 配置文件参数化
    详解自定义类型:结构体,位段,枚举,联合
    如何安装不同版本的go
    Postgres 和 MySQL 应该怎么选?
    Python中的单例类
    小码哥学习笔记:APP内存管理
    用 Go 快速开发一个 RESTful API 服务
  • 原文地址:https://blog.csdn.net/u013318019/article/details/134510411