C++ static_assert

assert是运行期断言,它用来发现运行期间的错误
stastic_assert为了弥补assert和error的不足,可以作编译期的静态检查

#include <iostream>

int main()
{
   static_assert(sizeof(char) == 2, "hello furong");

   return 0;
}
$ g++ assert.cpp -std=c++11
assert1.cpp: In function ‘int main()’:
assert1.cpp:5:4: error: static assertion failed: hello furong
    static_assert(sizeof(char) == 2, "hello furong");
posted @ 2022-09-02 16:47  thomas_blog  阅读(53)  评论(0编辑  收藏  举报