Sun Studio 12 Update 1: C++ User's Guide

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 Appendix B, Pragmas for details on the pragmas that the C++ compiler recognizes.

Sun C++ also supports the C99 keyword _Pragma. The two invocations


#pragma dumpmacros(defs)     
_Pragma("dumpmacros(defs)") 

are equivalent. 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):\
                        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);

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

Table A–2 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 #error

The #error directive no longer continues compilation after issuing a warning. The previous behavior of the directive was to issue a warning and continue compilation. The new behavior, consistent with other compilers, is to issue an error message and immediately halt compilation. The compiler quits and reports the failure.