名前空間
変種
操作

Replacing text macros

提供: cppreference.com

 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
 
プリプロセッサはテキストマクロの置換をサポートしています。関数のようなテキストマクロ置換もサポートされています.
Original:
The preprocessor supports text macro replacement. Function-like text macro replacement is also supported.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] 構文

#define identifier replacement-list (1)
#define identifier( parameters ) replacement-list (2)
#define identifier( parameters, ... ) replacement-list (3)
#define identifier( ... ) replacement-list (4)
#undef identifier (5)

[編集] 説明

[編集] NJディレクティブ

#defineディレクティブは、必要に応じてさら​​に処理することができidentifieridentifierの連続するすべての出現箇所を置換するようにコンパイラに指示しているマクロとしてreplacement-listを定義します。識別子は既にマクロのいずれかのタイプとして定義されている場合、プログラムは不適格です.
Original:
The #define directives define the identifier as macro, that is instruct the compiler to replace all successive occurrences of identifier with replacement-list, which can be optionally additionally processed. If the identifier is already defined as any type of macro, the program is ill-formed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集] オブジェクトのようなマクロ
オブジェクトのようなマクロはidentifierで定義さreplacement-listが発生するたびに交換してください。まさにそれが好き#defineディレクティブのバージョン(1)は動作します.
Original:
Object-like macros replace every occurrence of defined identifier with replacement-list. Version (1) of the #define directive behaves exactly like that.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集] 関数形式のマクロ
関数形式のマクロは、さらにその後identifierreplacement-listのいずれかの対応する箇所を置き換える引数の数を取って、parametersで定義さreplacement-listの各オカレンスを交換してください。マクロ定義(parameters)の引数の数やプログラムは不適格であるとして、引数の数が同じでなければなりません。識別子は機能表記に含まれていない場合、すなわち、それ自体の後に括弧がありませんが、それがすべてでは置き換えられません.
Original:
Function-like macros replace each occurrence of defined identifier with replacement-list, additionally taking a number of arguments, which then replace corresponding occurrences of any of the parameters in the replacement-list. The number of arguments must be the same as the number of arguments in macro definition (parameters) or the program is ill-formed. If the identifier is not in functional-notation, i.e. does not have parentheses after itself, it is not replaced at all.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#defineディレクティブのバージョン(2)は単純な関数のようなマクロを定義しています.
Original:
Version (2) of the #define directive defines a simple function-like macro.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#defineディレクティブのバージョン(3)は、可変個数の引数を持つ関数のようなマクロを定義しています。追加の引数は、次に交換する識別子で指定された引数に置き換えられ__VA_ARGS__識別子を使用してアクセスすることができます.
Original:
Version (3) of the #define directive defines a function-like macro with variable number of arguments. The additional arguments can be accessed using __VA_ARGS__ identifier, which is then replaced with arguments, supplied with the identifier to be replaced.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#defineディレクティブのバージョン(4)は、可変数の引数が、ない通常の引数を持つ関数のようなマクロを定義しています。引数は、次に交換する識別子で指定された引数に置き換えられ__VA_ARGS__識別子を使用してアクセスすることができます.
Original:
Version (4) of the #define directive defines a function-like macro with variable number of arguments, but no regular arguments. The arguments can be accessed only with __VA_ARGS__ identifier, which is then replaced with arguments, supplied with identifier to be replaced.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] NJとNJオペレーター

関数のようなマクロでは、#内の識別子の前にreplacement-list演算子はパラメータ交換を通じて識別子を実行して、引用符で囲んで、その結果を、効果的に文字列リテラルを作成する。また、プリプロセッサがあれば、埋め込まれた文字列リテラルを囲む引用符をエスケープするbackslahesを追加し、必要に応じて文字列内のバックスラッシュを倍増させます。すべての先頭と末尾の空白をが削除され、テキストの途中に空白の任意のシーケンス(なく埋め込まれた文字列リテラル内で)は、1つのスペースに縮小されます。この操作は、 "文字列化"と呼ばれています。文字列化の結果が有効な文字列リテラルでない場合、動作は未定義です.
Original:
In function-like macros, a # operator before an identifier in the replacement-list runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal. In addition, the preprocessor adds backslahes to escape the quotes surrounding embedded string literals, if any, and doubles the backslashes within the string as necessary. All leading and trailing whitespace is removed, and any sequence of whitespace in the middle of the text (but not inside embedded string literals) is collapsed to a single space. This operation is called "stringification". If the result of stringification is not a valid string literal, the behavior is undefined.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
##内の任意の2つの連続した​​識別子間replacement-list演算子は、2つの識別子とを連結した結果でパラメータ置換を実行します。この操作を "連結"や "貼り付けトークン"と呼ばれています。唯一の有効なトークンを形成するトークンが一緒に貼り付けることができます:より長い識別子を形成識別子番号を形成する数字、または演算子+=そのフォーム+=を。マクロ置換を考慮する前にコメントをテキストから削除されているため、コメントはペースト/*で作成することはできません。連結の結果が有効なトークンでない場合、動作は未定義です.
Original:
A ## operator between any two successive identifiers in the replacement-list runs parameter replacement on the two identifiers and then concatenates the result. This operation is called "concatenation" or "token pasting". Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier, digits that form a number, or operators + and = that form a +=. A comment cannot be created by pasting / and * because comments are removed from text before macro substitution is considered. If the result of concatenation is not a valid token, the behavior is undefined.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] NJディレクティブ

#undefディレクティブはidentifierディレクティブによってidentifierの以前の定義を取り消しさ#defineを未定義にします。識別子がマクロが関連付けられていない場合は、ディレクティブは無視されます.
Original:
The #undef directive undefines the identifier, that is cancels previous definition of the identifier by #define directive. If the identifier does not have associated macro, the directive is ignored.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 定義済みマクロ

次のマクロ名は、任意の変換単位で事前に定義されています.
Original:
The following macro names are predefined in any translation unit.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
__cplusplus
値199711L(C + + 11時まで)または201103L(C + + 11以来)に展開されます
Original:
expands to value 199711L(C + + 11時まで) or 201103L(C + + 11以来)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__STDC_HOSTED__
(C++11)
実装は(OSの下で実行されます)でホストされている場合は自立場合1(OSなしで実行されます)、整数定数0に展開されます
Original:
expands to the integer constant 1 if the implementation is hosted (runs under an OS), 0 if freestanding (runs without an OS)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__FILE__
リテラル文字列として、現在のファイルの名前に展開されます
Original:
expands to the name of the current file, as a character string literal
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__LINE__
ソースファイルの行番号を、整数定数に展開されます
Original:
expands to the source file line number, an integer constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__DATE__
翻訳の日付形式 "Mmm dd yyyy"のリテラル文字列に展開されます。 std::asctime()によって生成されたかのように月の名前です
Original:
expands to the date of translation, a character string literal of the form "Mmm dd yyyy". The name of the month is as if generated by std::asctime()
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__TIME__
翻訳時に展開され、時刻を "hh:mm:ss"のの文字列リテラル
Original:
expands to the time of translation, a character string literal of the form "hh:mm:ss"
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
次の追加のマクロ名は、実装によって事前定義されたかもしれません.
Original:
The following additional macro names may be predefined by the implementations.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
__STDC__
実装定義の値(存在する場合)
Original:
implementation-defined value, if present
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__STDC_VERSION__
(C++11)
実装定義の値(存在する場合)
Original:
implementation-defined value, if present
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__STDC_ISO_10646__
(C++11)
yyyymmLがUnicodeを使用している場合、日付はサポートされているUnicodeの最新のリビジョンを示し、フォームwchar_tの整数定数に展開されます
Original:
expands to an integer constant of the form yyyymmL, if wchar_t uses Unicode, the date indicates the latest revision of Unicode supported
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__STDC_MB_MIGHT_NEQ_WC__
(C++11)
基本的な文字セットのワイド文字エンコーディングは、その狭いエンコーディングと一致しない場合がある場合1に展開されます
Original:
expands to 1 if wide character encoding of the basic character set may not equal their narrow encoding
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__STDCPP_STRICT_POINTER_SAFETY__
(C++11)
実装は厳しい1を持っている場合std::pointer_safetyに展開されます
Original:
expands to 1 if the implementation has strict std::pointer_safety
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
__STDCPP_THREADS__
(C++11)
プログラムは、複数の実行スレッドを持つことができる場合1に展開されます
Original:
expands to 1 if the program can have more than one thread of execution
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(マクロ定数)
これらのマクロの値(ただし、__FILE____LINE__)翻訳​​単位内では一​​定のままである。これらのマクロを再定義または未定義にしようとすると、未定義の動作が発生.
Original:
The values of these macros (except for __FILE__ and __LINE__) remain constant throughout the translation unit. Attempts to redefine or undefine these macros result in undefined behavior.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
注:すべての関数本体のスコープ内で、実装定義の形式で関数の名前を保持している静的な文字配列として定義__func__(C + + 11以来)という名前の特別な関数ローカル定義済みの変数が存在する。これは、プリプロセッサマクロではありませんが、それは__FILE____LINE__などと一緒に使用されるassertバイ.
Original:
Note: in the scope of every function body, there is a special function-local predefined variable named __func__(C + + 11以来), defined as a static character array holding the name of the function in implementation-defined format. It is not a preprocessor macro, but it is used together with __FILE__ and __LINE__, e.g. by assert.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

#include <iostream>
 
//make function factory and use it
#define FUNCTION(name, a) int fun_##name() { return a;}
 
FUNCTION(abcd, 12);
FUNCTION(fff, 2);
FUNCTION(kkk, 23);
 
#undef FUNCTION
#define FUNCTION 34
#define OUTPUT(a) std::cout << #a << '\n'
 
int main()
{
    std::cout << "abcd: " << fun_abcd() << '\n';
    std::cout << "fff: " << fun_fff() << '\n';
    std::cout << "kkk: " << fun_kkk() << '\n';
    std::cout << FUNCTION << '\n';
    OUTPUT(million);               //note the lack of quotes
}

Output:

abcd: 12
fff: 2
kkk: 23
34
million