用C静态断言 [英] Static assert in C

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

问题描述

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

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

推荐答案

C-1X增加了_Static_assert关键字。

C-1x adds the _Static_assert keyword.

这似乎要实现GCC-4.6:

This seems to be implemented in gcc-4.6:

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

第一个槽必须是整型常量前pression。第二个插槽是一个常量字符串文字可以是长( _Static_assert(0,L厄运的说法!))。

我要指出,这也将在最新版本的铿锵的实现。

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

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

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