Go to main content
Oracle® Developer Studio 12.6: C++ User's Guide

Exit Print View

Updated: July 2017

2.5 Preprocessing Directives and Names

This section discusses information about preprocessing directives that is specific to the C++ compiler.

2.5.1 Pragmas

The preprocessor directive pragma is part of the C++ standard but the form, content, and meaning of pragmas is different for every compiler. See Pragmas for details on the pragmas that the C++ compiler recognizes.

Oracle Developer Studio C++ also supports the C99 keyword _Pragma. The following two invocations are equivalent:

#pragma dumpmacros(defs)     

To use _Pragma instead of #pragma, write the pragma text as a literal string enclosed in parentheses as the one argument of the _Pragma keyword.

2.5.2 Macros With a Variable Number of Arguments

The C++ compiler accepts #define preprocessor directives of the following form.

#define identifier (...) replacement-list
#define identifier (identifier-list, ...) replacement-list

If the macro parameter list ends with an ellipsis, an invocation of the macro is allowed to have more arguments than there are macro parameters. The additional arguments are collected into a single string, including commas, that can be referenced by the name __VA_ARGS__ in the macro replacement list.

The following example demonstrates how to use a variable-argument-list macro.

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define showlist(...) puts(#__VA_ARGS__)
#define report(test, ...) ((test)?puts(#test):\
debug(“X = %d\n”,x);
showlist(The first, second, and third items.);
report(x>y, “x is %d but y is %d”, x, y);

which results in the following:

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));

2.5.3 Predefined Names

–Dname[=def] in the appendix shows the predefined macros. You can use these values in such preprocessor conditionals as #ifdef.The +p option prevents the automatic definition of the sun, unix, sparc, and i386 predefined macros.

2.5.4 Warnings and Errors

The #error and #warning preprocessor directives can be used to generate compile-time diagnostics.

#error token-string

Issue error diagnostic token-string and terminate compilation

#warning token-string

Issue warning diagnostic token-string and continue compilation.