0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C9x な Legacy な C での static_assert

Posted at

工場の機材とか相手にするとたまにあるんですよね。

#define static_assert(cond, msg) \
typedef int compile_time_assertion_[(cond) ? 1 : -1]

cond の評価結果が負になると三項演算子の : の次の式が採用されて、

typedef int compile_time_assertion_[-1];

と展開されて、-1 を配列のサイズに採用できないよ!というエラーになります。
(ただし、cond はコンパイル時定数である必要があります)

メッセージは出ません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?