「More C++ Idioms/inline ガードマクロ(Inline Guard Macro)」の版間の差分

削除された内容 追加された内容
Yak! (トーク | 投稿記録)
現内容を翻訳
1 行
=<center>inline ガードマクロ(Inline Guard Macro)</center>=
=== Intent意図 ===
コンパイラのコマンドライン指定によるマクロ定義切り替えを利用して、関数を inline にするかどうかを簡単に制御する。
To conveniently control inline-ness of functions using a compiler command line macro definition switch.
 
=== Also Known As別名 ===
 
=== Motivation動機 ===
デバッグ目的のため、プログラム全体で関数の inline 化を止める必要がある場合が多い。
For debugging purpose, it is often necessary to turn off inlining of functions throughout the program. But for release version, inline functions are desirable. This indicates a need of a quick way of turning inline-ness on/off as and when desired. Moreover, such functions should be defined in header files when they are inlined and otherwise should be in the source (.cpp) file. If non-inline functions are in header files, almost always it ends up creating multiple definitions of the function. On the other hand, if inline functions are not in header files then compilation units can't fine them. In both the cases linker throws errors.
しかし、リリースバージョンでは、inline 関数が望ましい。
必要な時必要になり次第 inline の有効、無効を素早く切り替える方法が必要である。
その上、そのような関数は、inline 化される時にはヘッダファイル中で定義されるべきであり、inline 化されない時にはソースファイル(.cpp)中で定義されるべきである。
非 inline 関数がヘッダファイルにある場合、ほとんどいつでも、関数定義を複数作ってしまうことになる。一方、inline 関数がヘッダファイルにない場合、複数の翻訳単位で利用することができない。いずれの場合も、リンカがエラーを出力する。
 
そのため、柔軟な inline 化の方法が望ましい場合が多いが、C++ ではちょっとしたマクロの魔法なしには対応できない。inlineガードマクロイディオムは、これを実現する。
Therefore, a flexible way of inlining is often desirable but C++ language does not support it without some macro magic. The Inline Guard Macro idiom achieves this.
 
=== Solution and Sample Code解法とサンプルコード ===
全ての inline 関数を、.ipp ファイルという別のファイルに置き、各関数をマクロ INLINE で修飾することが解法となる。
The solution is to put all the inline functions in a separate file called .ipp file and decorate each function with a macro INLINE. Header file and the implementation file is create as usual and the .ipp file in selectively included in one of the two files (header or implementation) depending upon whether inlining is desired. An example of a class Test is given below.
ヘッダファイルと実装ファイルはいつも通り作成し、.ipp ファイルは、inline 化が望まれるかどうかに応じて、これら2つのうち1つのファイル(つまりヘッダファイルか実装ファイルか)内で選択的にインクルードされる。
下記はクラス Test の例である。
<source lang="cpp">
// test.ipp fileファイル
INLINE void Test::func()
{}
 
// test.hpp fileファイル
#ifndef __TEST_H // Note include guards.インクルードガード
#define __TEST_H
 
28 ⟶ 34行目:
 
#ifdef _INLINE_
#define INLINE inline // Define INLINE as inline (the keywordキーワード)として定義する
#include "test.ipp" // It is included only when _INLINE_ is definedが定義されている時のみインクルードされる
#endif
 
35 ⟶ 41行目:
 
//test.cpp file
#include "test.hpp" // Include header file as usual.いつも通りヘッダファイルをインクルードする
 
#ifndef _INLINE_
#define INLINE // INLINE is defined as empty stringは空文字列として定義される
#include "test.ipp" // It is included only when _INLINE_ is NOT defined.が定義されて「いない」時のみインクルードされる
#endif
</source>
 
inline ガードマクロの使用により、_INLINE_ が定義されているかどうかによって test.ipp が test.cpp か test.hpp のいずれかに組み込まれる。
The effect of using Include Guard Macro is that depending upon whether _INLINE_ is defined or not, test.ipp gets merged with either test.cpp or test.hpp. When it gets merged with test.cpp, functions are not inlined because INLINE is defined as an empty string. On the other hand when test.ipp is merged with test.hpp, INLINE is defined as inline (the keyword). Now, what remains is to define _INLINE_ macro.
test.cpp に組み込まれた場合、INLINE が空文字列として定義されるため関数は inline 化されない。一方、test.hpp に組み込まれた場合、INLINE が inline (キーワード)として定義される。今や残りは _INLINE_ マクロを定義することだけである。
Generally, all modern C/C++ compilers allow defining macros at command line. For example to compile the above program on gcc using inlining, '''-D _INLINE_''' option is used. If no such macro is defined, the functions are automatically treated as non-inline and program compiles.
一般的に、全ての現代的な C/C++ コンパイラはコマンドラインでマクロを定義することができる。例えば、上記プログラムを gcc で inline 有効でコンパイルする場合、'''-D _INLINE_''' オプションを使用する。そのようなマクロが定義されていない場合、関数は自動的に非 inline として扱われ、プログラムがコンパイルされる。
 
=== Known Uses既知の利用 ===
* ACE (Adaptive Communication Environment)
* TAO (The ACE ORB)
 
=== Related Idioms関連するイディオム ===
* [[More C++ Idioms/インクルードガードマクロ(Include Guard Macro)|インクルードガードマクロ(Include Guard Macro)]]
* [[More C++ Idioms/export ガードマクロ(Export Guard Macro)|export ガードマクロ(Export Guard Macro)]]
[[en:More C++ Idioms/Inline Guard Macro]]
 
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}]]