C中的静态断言 [英] Static assert in C

查看:33
本文介绍了C中的静态断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C(不是 C++)中实现编译时静态断言的最佳方法是什么,特别强调 GCC?

What's the best way to achieve compile time static asserts in C (not C++), with particular emphasis on GCC?

推荐答案

C11 标准添加了 _Static_assert 关键字.

C11 standard adds the _Static_assert keyword.

这是自 gcc-4.6 开始实施:

_Static_assert (0, "assert1"); /* { dg-error "static assertion failed: "assert1"" } */

第一个槽需要是一个整数常量表达式.第二个槽是一个可以很长的常量字符串文字 (_Static_assert(0, L"assertion of doom!")).

The first slot needs to be an integral constant expression. The second slot is a constant string literal which can be long (_Static_assert(0, L"assertion of doom!")).

我应该注意,这也在最新版本的 clang 中实现.

I should note that this is also implemented in recent versions of clang.

这篇关于C中的静态断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆