タグ

Cとmacroに関するMonMonMonのブックマーク (2)

  • Linuxカーネルで学ぶC言語のマクロ - 覚書

    はじめに 記事は電子書籍版もあります。 linuxカーネルはC言語のマクロを駆使して書かれています。それらのうち、凝ったマクロになじみの無い人には初見では意図がわからない&わかってみれば面白いであろうものをいくつか紹介いたします。対象読者は、C言語のユーザだけれども、マクロは定数定義くらいにしか使わないというライトなマクロユーザです。 マクロを使用する場所に依存するエラーを防ぐ 次のマクロは、二つの引き数の値を置換するだけの単純なものです。 #define swap(a, b) \ do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 注目すべきはマクロの定義全体を囲んでいるdo { ... } while (0)という表記です。初見の人には何のことかわからないと思います。考えられる最も単純な定義から遡って、なぜこ

    Linuxカーネルで学ぶC言語のマクロ - 覚書
  • Overloading Macro on Number of Arguments

    I have two macros FOO2 and FOO3: #define FOO2(x,y) ... #define FOO3(x,y,z) ... I want to define a new macro FOO as follows: #define FOO(x,y) FOO2(x,y) #define FOO(x,y,z) FOO3(x,y,z) But this doesn't work because macros do not overload on number of arguments. Without modifying FOO2 and FOO3, is there some way to define a macro FOO (using __VA_ARGS__ or otherwise) to get the same effect of dispatchi

    Overloading Macro on Number of Arguments
  • 1