C++ のコメントを C ソース・ファイルで認識させるには、このオプションを使用します。
デフォルト設定は、以下のようにそれぞれ異なります。
__C99_CPLUSCMT コンパイラー・マクロは、cpluscmt が選択される際に定義されます。
文字シーケンス // は、ヘッダー名、文字定数、ストリング・リテラル、またはコメントを除いて、 C++ のコメントの開始となります。コメントはネストしません。また、マクロ置き換えは、コメント内では実行されません。以下の文字シーケンスは、次の C++ コメント内で無視されます。
- //
- /*
- */
C++ のコメントの形式は、//text です。文字シーケンスの 2 つのスラッシュ (//) は、2 つの間に何もはさまずに隣接していなければなりません。スラッシュの右側から改行文字で示される論理ソース行の最後までが、すべてコメントとして扱われます。// 区切り文字は、行内の任意の位置に置くことができます。
// コメントは C89 の一部ではありません。 -qcpluscmt を指定すると、以下の有効な C89 プログラムの結果が誤りになります。
main() { int i = 2; printf("%i¥n", i //* 2 */ + 1); }正しい答えは 2 (2 / 1) です。 -qcpluscmt を指定すると、結果は 3 (2 + 1) になります。
プリプロセッサーは、以下の方法でコメントすべてを処理します。
円記号 (¥) 文字を使用して複数の物理ソース行が 1 つの論理ソース行に結合されている場合には、コメントは、それらの物理ソース行にわたることができます。 3 文字表記 (??/) によって円記号を表すこともできます。
以下の例に、C++ のコメントの使用を示します。
// A comment that spans two ¥ physical source lines // A comment that spans two ??/ physical source lines
以下のソース・コード・フラグメントの場合:
int a; int b; // A comment that spans two ¥ physical source lines int c; // This is a C++ comment int d;
-P オプションの場合の出力は、以下のとおりです。
int a; int b; int c; int d;
-P -C オプションの場合の C89 モード出力は、以下のとおりです。
int a; int b; // A comment that spans two physical source lines int c; // This is a C++ comment int d;
-E オプションの場合の出力は、以下のとおりです。
int a; int b; int c; int d;
-E -C オプションの場合の C89 モード出力は、以下のとおりです。
#line 1 "fred.c" int a; int b; // a comment that spans two ¥ physical source lines int c; // This is a C++ comment int d;
-P -C オプションまたは -E -C オプションの場合の拡張モード出力は、以下のとおりです。
int a; int b; // A comment that spans two ¥ physical source lines int c; // This is a C++ comment int d;
以下のソース・コード・フラグメントの場合:
int a; #define mm 1 // This is a C++ comment on which spans two ¥ physical source lines int b; // This is a C++ comment int c;
-P オプションの場合の出力は、以下のとおりです。
int a; int b; int c;
-P -C オプションの場合の出力は、以下のとおりです。
int a; int b; // This is a C++ comment int c;
-E オプションの場合の出力は、以下のとおりです。
#line 1 "fred.c" int a; #line 4 int b; int c;
-E -C オプションの場合の出力は、以下のとおりです。
#line 1 "fred.c" int a; #line 4 int b; // This is a C++ comment int c;
以下のソース・コード・フラグメントの場合:
#define mm(aa) aa int a; int b; mm(// This is a C++ comment int blah); int c; // This is a C++ comment int d;
-P オプションの場合の出力は、以下のとおりです。
int a; int b; int blah; int c; int d;
-P -C オプションの場合の出力は、以下のとおりです。
int a; int b; int blah; int c; // This is a C++ comment int d;
-E オプションの場合の出力は、以下のとおりです。
#line 1 "fred.c" int a; int b; int blah; int c; int d;
-E -C オプションの場合の出力は、以下のとおりです。
#line 1 "fred.c" int a; int b; int blah; int c; // This is a C++ comment int d;
C++ のコメントがコメントとして認識されるように myprogram.c. so をコンパイルするには、次のように入力します。
xlc myprogram.c -qcpluscmt