C++11 標準では、C および C++ コンパイラーに対して共通プリプロセッサー・インターフェースを提供するために、複数の C99 プリプロセッサー機能が採用されています。 これにより、C ソース・ファイルを C++ コンパイラーに簡単に移植でき、旧式の C プリプロセッサーと C++ プリプロセッサーの間に存在するいくつかのわずかな意味の違いを除去できるため、プリプロセッサーの互換性の問題を解決し、プリプロセッサーが異なる動作を行うことを回避できます。
C89、C++98、および C++03 プリプロセッサーでは、int 型または unsigned int 型を持つ整数リテラルは、long または unsigned long に拡張されます。ただし、C99 および C++11 プリプロセッサーでは、 すべての signed 整数型および unsigned 整数型 (文字型を含む) は、XL C/C++ の通常の環境の下では long long または unsigned long long に拡張されます。
この機能を使用可能にして、-qnolonglong および -qlanglvl=noc99longlong の両方が -q32 モードまたは -q64 モードのいずれかで設定されている場合、プリプロセッサーはプリプロセッサー制御式のすべての整数リテラルおよび文字リテラルに対して、依然として long long または unsigned long long の表記を使用します。
#if L'¥x0' - L'¥x1' < 0
#error non-C++11 preprocessor arithmetic.
#else
#error C++11 preprocessor arithmetic! L'¥x0' and L'¥x1' are widened to¥
unsigned long long
#endif
#if ~0ull == 0u + ~0u
#error C++11 preprocessor arithmetic! 0u has the same representation as 0ull,¥
hence ~0ull == 0u + ~0u
#else
#error non-C++11 preprocessor arithmetic. 0ul does not have the same ¥
representation as 0ull, hence ~0ull != 0u + ~0u
#endif
この機能を無効にして -qwarn0x を設定すると、C++11 プリプロセッサーは、#if ディレクティブおよび #elif ディレクティブの制御式を評価し、評価結果を非 C++11 プリプロセッサーの結果と比較します。結果が異なる場合、コンパイラーはプリプロセッサーの制御式の評価が、C++11 と非 C++11 の言語レベル間で異なることを警告します。
#include <wchar.h>
#include <stdio.h>
int main()
{
wprintf(L"Guess what? %ls¥n", "I can now concate" L"nate regular strings¥
and wide strings!");
printf("Guess what? %ls¥n", L"I can now concate" "nate strings¥
this way too!");
}
この例を実行すると、以下を出力します。Guess what? I can now concatenate regular strings and wide strings!
Guess what? I can now concatenate strings this way too!
//inc.C
#include "0x/mylib.h"
int main()
{
return 0;
}
この機能を使用可能にして、この例をコンパイルまたはプリプロセッシングすると、コンパイラーは以下の警告メッセージを出します。"inc.C", line 1.10: 1540-0893 (W) The header file name "0x/mylib.h"
in #include directive shall not start with a digit.
#line 1000000 //Valid in C++11, but invalid in C++98
int main()
{
return 0;
}
//w.C
//With -qnodollar, '$' is not part of the macro name,
//thus it begins the replacement list
#define A$B c
#define STR2( x ) # x
#define STR( x ) STR2( x )
char x[] = STR( A$B );
この機能を使用可能にし、-qnodollar を指定して、この例をコンパイルまたはプリプロセッシングすると、コンパイラーは以下の警告メッセージを出します。"w.C", line 1.10: 1540-0891 (W) Missing white space between
the identifier "A" and the replacement list.
#pragma comment(copyright, "IBM 2010")
_Pragma("comment(copyright, ¥"IBM 2010¥")")
_Pragma("comment(copyright, ¥"IBM 2010¥")")
int main()
{
return 0;
}
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define showlist(...) puts(#__VA_ARGS__)
#define report(test, ...) ((test)?puts(#test): printf(__VA_ARGS__))
debug("Flag");
debug("X = %d¥n", x);
showlist(The first, second, and third items.);
report(x>y, "x is %d but y is %d", x, y);
この例は、プリプロセッシング後に以下のコードに展開されます。fprintf(stderr, "Flag");
fprintf(stderr, "X = %d¥n", x);
puts("The first, second, and third items.");
((x>y)?puts("x>y"): printf("x is %d but y is %d", x, y));