IBM XL C/C++ Enterprise Edition for AIX, V9.0
Language Reference
SC23-5829-00
IBM XL C/C++ Enterprise Edition for AIX, V9.0
Language Reference
SC23-5829-00
Note!
Before using this information and the product it supports, be sure to read the general information under “Notices” on page 419.
First Edition
This edition applies to IBM XL C/C++ Enterprise Edition for AIX, V9.0 (Program number 5724-S71) and to all subsequent releases and modifications until otherwise indicated in new editions. Make sure you are using the correct edition for the level of the product.
© Copyright International Business Machines Corporation 1998, 2007. All rights reserved.
US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
Contents
About this document ...... vii The __thread storage class specifier . . . . .52
Who should read this document ...... vii Type specifiers ...... 52
How to use this document ...... vii Integral types...... 53
Language levels and language extensions . . . vii Boolean types ...... 54
How this document is organized ...... ix Floating-point types ...... 54
Conventions used in this document ...... ix Character types ...... 56
Related information ...... xii The void type ...... 57
IBM XL C/C++ publications ...... xii Compatibility of arithmetic types (C only) . . .57
Standards and specifications documents . . . xiii Vector types ...... 57
Other IBM publications ...... xiv User-defined types ...... 59
Other publications ...... xiv Structures and unions ...... 59
Technical support ...... xiv Enumerations...... 67
How to send your comments ...... xv Compatibility of structures, unions, and
enumerations (C only) ...... 70
typedef definitions ...... 71 Chapter 1. Scope and linkage . . . . .1
Type qualifiers ...... 72 Scope ...... 2
The __align type qualifier ...... 74 Block/local scope ...... 2
The const type qualifier ...... 76 Function scope ...... 3
The restrict type qualifier ...... 76 Function prototype scope ...... 3
The volatile type qualifier ...... 77 File/global scope ...... 3
Type attributes ...... 78 Examples of scope in C ...... 4
The aligned type attribute ...... 79 Class scope (C++ only) ...... 5
The packed type attribute ...... 80 Namespaces of identifiers ...... 6
The transparent_union type attribute (C only) . .80 Name hiding (C++ only) ...... 7
Program linkage ...... 7
Chapter 4. Declarators ...... 83 Internal linkage ...... 8
External linkage ...... 8 Overview of declarators ...... 83
No linkage ...... 9 Examples of declarators ...... 84
Language linkage (C++ only) ...... 10 Type names ...... 85
Pointers ...... 86
Pointer arithmetic ...... 87 Chapter 2. Lexical Elements . . . . .13
Type-based aliasing ...... 88 Tokens ...... 13
Compatibility of pointers (C only) ...... 89 Keywords ...... 13
Arrays ...... 90 Identifiers ...... 15
Variable length arrays ...... 91 Literals ...... 19
Compatibility of arrays ...... 92 Punctuators and operators ...... 31
References (C++ only) ...... 93 Source program character set ...... 32
Initializers ...... 93 Multibyte characters ...... 33
Initialization and storage classes ...... 94 Escape sequences ...... 34
Designated initializers for aggregate types (C The Unicode standard ...... 35
only) ...... 95 Digraph characters ...... 37
Initialization of vectors ...... 97 Trigraph sequences ...... 37
Initialization of structures and unions . . . .98 Comments ...... 38
Initialization of enumerations ...... 100
Initialization of pointers ...... 101
Chapter 3. Data objects and Initialization of arrays ...... 101
declarations ...... 41 Initialization of references (C++ only) . . . . 104
Overview of data objects and declarations . . . .41 Variable attributes ...... 105
Overview of data objects ...... 41 The aligned variable attribute ...... 107
Overview of data declarations and definitions . .43 The packed variable attribute ...... 108
Storage class specifiers...... 45 The mode variable attribute ...... 108
The auto storage class specifier ...... 46 The weak variable attribute ...... 108
The static storage class specifier ...... 46
The extern storage class specifier ...... 48 Chapter 5. Type conversions . . . . . 111
The mutable storage class specifier (C++ only) . .49 Arithmetic conversions and promotions . . . . 111
The register storage class specifier ...... 49
© Copyright IBM Corp. 1998, 2007 iii
Integral conversions ...... 112 Cast expressions ...... 154
Boolean conversions ...... 112 Cast operator () ...... 154
Floating-point conversions ...... 112 The static_cast operator (C++ only) . . . . . 157
Integral and floating-point promotions . . . .113 The reinterpret_cast operator (C++ only) . . . 158
Lvalue-to-rvalue conversions ...... 115 The const_cast operator (C++ only) . . . . . 159
Pointer conversions ...... 115 The dynamic_cast operator (C++ only) . . . . 160
Conversion to void* ...... 116 Compound literal expressions ...... 162
Reference conversions (C++ only) ...... 117 new expressions (C++ only) ...... 163
Qualification conversions (C++ only) . . . . .117 Placement syntax ...... 164
Function argument conversions ...... 117 Initialization of objects created with the new
operator ...... 166
Chapter 6. Expressions and operators 119 Handling new allocation failure ...... 166
delete expressions (C++ only) ...... 167 Lvalues and rvalues ...... 119
throw expressions (C++ only) ...... 168 Primary expressions ...... 121
Label value expressions ...... 168 Names ...... 121
Operator precedence and associativity . . . . . 168 Literals ...... 122
Integer constant expressions ...... 122
Identifier expressions (C++ only) ...... 122 Chapter 7. Statements ...... 173
Parenthesized expressions ( ) ...... 123 Labeled statements ...... 173
Scope resolution operator :: (C++ only) . . . . 124 Locally declared labels ...... 174
Function call expressions ...... 125 Labels as values ...... 174
Member expressions ...... 126 Expression statements ...... 175
Dot operator ...... 126 Resolution of ambiguous statements. . . . . 175
Arrow operator −> ...... 126 Block statements ...... 176
Unary expressions ...... 126 Example of blocks ...... 177
Increment operator ++ ...... 127 Statement expressions ...... 177
Decrement operator −− ...... 128 Selection statements ...... 178
Unary plus operator + ...... 128 The if statement ...... 178
Unary minus operator - ...... 128 The switch statement ...... 179
Logical negation operator ! ...... 129 Iteration statements ...... 183
Bitwise negation operator ~ ...... 129 The while statement ...... 183
Address operator & ...... 130 The do statement ...... 184
Indirection operator * ...... 131 The for statement ...... 185
The typeid operator (C++ only) ...... 131 Jump statements ...... 187
The __alignof__ operator ...... 133 The break statement ...... 187
The sizeof operator ...... 133 The continue statement ...... 188
The typeof operator ...... 135 The return statement ...... 189
The __real__ and __imag__ operators . . . . 136 The goto statement ...... 190
The vec_step operator ...... 137 Null statement ...... 192
Binary expressions ...... 137 Inline assembly statements ...... 192
Assignment operators ...... 138 Examples of inline assembly statements . . . 195
Multiplication operator * ...... 140 Restrictions on inline assembly statements. . . 196
Division operator / ...... 140
Remainder operator % ...... 141 Chapter 8. Functions ...... 197
Addition operator + ...... 141 Function declarations and definitions . . . . . 197
Subtraction operator − ...... 141 Function declarations ...... 198
Bitwise left and right shift operators << >> . . 142 Function definitions ...... 198
Relational operators < > <= >= ...... 142 Examples of function declarations . . . . . 199
Equality and inequality operators == != . . . 144 Examples of function definitions ...... 200
Bitwise AND operator & ...... 145 Compatible functions ...... 200
Bitwise exclusive OR operator ^ ...... 145 Multiple function declarations ...... 201
Bitwise inclusive OR operator | ...... 146 Function storage class specifiers ...... 202
Logical AND operator && ...... 146 The static storage class specifier ...... 202
Logical OR operator || ...... 147 The extern storage class specifier ...... 202
Array subscripting operator [ ] ...... 148 Function specifiers ...... 204
Comma operator , ...... 149 The inline function specifier ...... 204
Pointer to member operators .* −>* (C++ only) 151 Function return type specifiers ...... 209
Conditional expressions ...... 151 Function return values ...... 209
Types in conditional C expressions . . . . . 152 Function declarators ...... 210
Types in conditional C++ expressions . . . . 152 Parameter declarations ...... 211
Examples of conditional expressions . . . . . 153
iv XL C/C++ Language Reference
Function attributes ...... 214 Chapter 12. Class members and
The alias function attribute ...... 215 friends (C++ only) ...... 263
The always_inline function attribute . . . . . 216 Class member lists ...... 263
The const function attribute ...... 216 Data members ...... 264
The format function attribute ...... 217 Member functions ...... 265
The format_arg function attribute . . . . . 218 Inline member functions ...... 265
The noinline function attribute ...... 218 Constant and volatile member functions . . . 266
The noreturn function attribute ...... 218 Virtual member functions ...... 266
The pure function attribute ...... 219 Special member functions ...... 267
The weak function attribute ...... 219 Member scope ...... 267
The main() function ...... 219 Pointers to members ...... 268
Function calls ...... 220 The this pointer ...... 270
Pass by value ...... 221 Static members ...... 273
Pass by reference ...... 222 Using the class access operators with static
Allocation and deallocation functions (C++ only) 223 members ...... 273
Default arguments in C++ functions ...... 224 Static data members ...... 274
Restrictions on default arguments . . . . . 225 Static member functions ...... 276
Evaluation of default arguments ...... 226 Member access ...... 277
Pointers to functions ...... 227 Friends ...... 280
Nested functions ...... 228 Friend scope ...... 282
Friend access ...... 284
Chapter 9. Namespaces (C++ only) 229
Defining namespaces ...... 229 Chapter 13. Inheritance (C++ only) 285
Declaring namespaces ...... 229 Derivation ...... 287
Creating a namespace alias ...... 229 Inherited member access ...... 290
Creating an alias for a nested namespace . . . . 230 Protected members ...... 290
Extending namespaces ...... 230 Access control of base class members . . . . 291
Namespaces and overloading ...... 231 The using declaration and class members . . . . 292
Unnamed namespaces ...... 231 Overloading member functions from base and
Namespace member definitions ...... 233 derived classes ...... 293
Namespaces and friends ...... 233 Changing the access of a class member . . . . 295
The using directive ...... 234 Multiple inheritance ...... 296
The using declaration and namespaces . . . . . 234 Virtual base classes ...... 297
Explicit access ...... 235 Multiple access ...... 298
Ambiguous base classes ...... 299
Chapter 10. Overloading (C++ only) 237 Virtual functions ...... 303
Overloading functions ...... 237 Ambiguous virtual function calls ...... 306
Restrictions on overloaded functions . . . . 238 Virtual function access ...... 308
Overloading operators ...... 239 Abstract classes ...... 308
Overloading unary operators ...... 242
Overloading increment and decrement operators 243 Chapter 14. Special member functions
Overloading binary operators ...... 244
(C++ only) ...... 311
Overloading assignments ...... 245
Overview of constructors and destructors . . . .311 Overloading function calls ...... 246
Constructors ...... 313 Overloading subscripting ...... 247
Default constructors ...... 313 Overloading class member access ...... 248
Explicit initialization with constructors . . . . 314 Overload resolution ...... 249
Initialization of base classes and members . . . 316 Implicit conversion sequences ...... 250
Construction order of derived class objects . . 319 Resolving addresses of overloaded functions 251
Destructors ...... 320
Pseudo-destructors ...... 322
Chapter 11. Classes (C++ only) . . . . 253 User-defined conversions ...... 323
Declaring class types ...... 254 Conversion constructors ...... 324
Using class objects ...... 254 The explicit specifier ...... 326
Classes and structures ...... 256 Conversion functions ...... 326
Scope of class names ...... 257 Copy constructors ...... 327
Incomplete class declarations ...... 258 Copy assignment operators ...... 329
Nested classes ...... 258
Local classes ...... 260
Chapter 15. Templates (C++ only) . . . 331
Local type names ...... 261
Template parameters ...... 332
Type template parameters ...... 332
Contents v
Non-type template parameters ...... 332 The #define directive ...... 386
Template template parameters ...... 333 The #undef directive ...... 390
Default arguments for template parameters . . 333 The # operator ...... 391
Template arguments ...... 334 The ## operator ...... 392
Template type arguments ...... 334 Standard predefined macro names . . . . . 392
Template non-type arguments ...... 335 File inclusion directives ...... 394
Template template arguments ...... 337 The #include directive ...... 394
Class templates ...... 338 The #include_next directive ...... 395
Class template declarations and definitions . . 339 Conditional compilation directives ...... 396
Static data members and templates . . . . . 340 The #if and #elif directives ...... 397
Member functions of class templates . . . . 340 The #ifdef directive ...... 398
Friends and templates ...... 341 The #ifndef directive ...... 399
Function templates ...... 342 The #else directive ...... 399
Template argument deduction ...... 343 The #endif directive ...... 399
Overloading function templates ...... 348 Message generation directives ...... 400
Partial ordering of function templates . . . . 349 The #error directive ...... 401
Template instantiation ...... 350 The #warning directive ...... 401
Implicit instantiation ...... 351 The #line directive ...... 401
Explicit instantiation ...... 352 Assertion directives ...... 403
Template specialization ...... 353 Predefined assertions ...... 403
Explicit specialization ...... 353 The null directive (#) ...... 404
Partial specialization ...... 358 Pragma directives ...... 404
Name binding and dependent names . . . . . 360 The _Pragma preprocessing operator . . . . 404
The typename keyword ...... 362 Standard pragmas ...... 405
The template keyword as qualifier ...... 362
Appendix A. The IBM XL C language
Chapter 16. Exception handling (C++ extensions ...... 407
only) ...... 365 C99 features as extensions to C89 ...... 407
try blocks ...... 365 Extensions for Unicode support ...... 409
Nested try blocks ...... 366 Extensions for GNU C compatibility ...... 409
catch blocks ...... 367 Extensions for vector processing support . . . .411
Function try block handlers ...... 368 Extensions for decimal floating-point support . .411
Arguments of catch blocks ...... 371
Matching between exceptions thrown and Appendix B. The IBM XL C++
caught ...... 371 language extensions ...... 413
Order of catching ...... 371 General IBM extensions ...... 413 throw expressions ...... 373 Extensions for C99 compatibility ...... 413
Rethrowing an exception ...... 373 Extensions for Unicode support ...... 414
Stack unwinding ...... 374 Extensions for GNU C compatibility ...... 414
Exception specifications ...... 376 Extensions for GNU C++ compatibility . . . . . 416
Special exception handling functions . . . . . 378 Extensions for vector processing support . . . . 417
The unexpected() function ...... 379 Extensions for decimal floating-point support . . 417
The terminate() function ...... 380
The set_unexpected() and set_terminate()
Notices ...... 419 functions ...... 381
Trademarks and service marks ...... 421 Example using the exception handling functions 381
Industry standards ...... 421
Chapter 17. Preprocessor directives 385
Index ...... 423 Macro definition directives ...... 385
vi XL C/C++ Language Reference
About this document
® This document describes the syntax, semantics, and IBM XL C/C++ Enterprise
® Edition for AIX implementation of the C and C++ programming languages.
Although the XL C and XL C++ compilers conform to the specifications
maintained by the ISO standards for the C and C++ programming languages, the
compilers also incorporate many extensions to the core languages. These extensions
have been implemented with the aims of enhancing usability in specific operating
environments, supporting compatibility with other compilers, and supporting new
® hardware capabilities. For example, on UNIX platforms, many language
constructs have been added for compatibility with the GNU Compiler Collection
(GCC), to maximize portability between the two development environments.
Who should read this document
This document is a reference for users who already have experience programming
applications in C or C++. Users new to C or C++ can still use this document to
find information on the language and features unique to XL C/C++; however, this
reference does not aim to teach programming concepts nor to promote specific
programming practices.
How to use this document
Unless indicated otherwise, all of the text in this reference pertains to both C and
C++ languages. Where there are differences between languages, these are indicated
through qualifying text and other graphical elements (see below for the
conventions used).
While this document covers both standard and implementation-specific features, it
does not include the following topics:
v Standard C and C++ library functions and headers. For standard C++ library
documentation, refer to the Standard C++ Library Reference. For information on
the standard C library, refer to your AIX operating system documentation.
v Constructs for writing multi-threaded programs, including IBM SMP directives,
OpenMP directives and functions and POSIX Pthread functions. For reference
documentation on IBM SMP and OpenMP constructs, see the XL C/C++ Compiler
Reference; for documentation on Pthreads library functions, refer to your AIX
documentation.
v Compiler pragmas, predefined macros, and built-in functions. These are
described in the XL C/C++ Compiler Reference.
Language levels and language extensions
The C and C++ languages described in this reference are based on the standards
listed in “Standards and specifications documents” on page xiii.
″ ″ We refer to the following language specifications as base language levels in order
to introduce the notion of an extension to a base. In this context the base language
levels refer to the following specifications:
v Standard C++
v C++98
v C99
© Copyright IBM Corp. 1998, 2007 vii
v C89
This document uses the term K&R C to refer to the C language plus the generally
accepted extensions produced by Brian Kernighan and Dennis Ritchie that were in
use prior to the ISO standardization of C.
In addition to the features supported by the base levels, XL C/C++ contains
language extensions that enhance usability and facilitate porting programs to
different platforms, including:
v Extensions to C++ to support C99 standard features
v Extensions related to GNU C and C++
v Extensions supporting the AltiVec Programming Interface
You can control the language level to be used for compilation through several
mechanisms, including:
v various invocation commands
v the -qlanglvl option
With a few exceptions, almost all of the language extensions are supported when
you compile using the basic invocation commands xlc (for C) and xlc++ or xlC (for
C++).
The default language level for the xlc invocation command is extc99, which
includes all of the features introduced by the C99 standard, and most of the IBM
extensions described in this document. For a complete listing of the C extensions
and various methods for enabling them, see Appendix A, “The IBM XL C language
extensions,” on page 407.
The default language level for the xlC or xlc++ invocation command is extended,
which includes most of the IBM extensions described in this document, as well as
many C99 features. For a complete listing of the C++ extensions and methods for
enabling them, see Appendix B, “The IBM XL C++ language extensions,” on page
413.
For information on the various methods for controlling the language level for ″ ″ compilation, see Invoking the compiler and -qlanglvl in the XL C/C++ Compiler
Reference.
Extensions to C++ to support C99 standard features
The Standard C++ language specification does not include many of the features
specified in the C99 language standard. To promote compatibility and portability
between C99 and C++, the XL C++ compiler enables many of the C99 features that
are supported by the XL C compiler. Since these features extend Standard C++,
they are considered extensions to the base language. In this reference, unless the
text is marked to indicate that a feature is supported in C or C99 only, C99 features
also apply to C++. A complete list of C99 features supported in XL C++ is also
provided in “Extensions for C99 compatibility” on page 413.
Extensions to support decimal floating-point hardware
XL C/C++ supports the built-in C decimal floating-point types proposed in
Information Technology – Programming Languages – Extension for the programming
language C to support decimal floating-point arithmetic, ISO/IEC WDTR 24732. This
support is enabled with the -qdfp option. For compatibility with C, XL C++ also
supports these native types and literals. A list of these extensions is provided in
“Extensions for decimal floating-point support” on page 417.
viii XL C/C++ Language Reference
Extensions related to GNU C and C++
Certain language extensions that correspond to GNU C and C++ features are
implemented to facilitate portability. These include extensions to C89, C99, C++98,
and Standard C++. Throughout this document, the text indicates the IBM
extensions that have been implemented for compatibility with GNU C and C++; a
complete list of these is also provided in “Extensions for GNU C compatibility” on
page 409 and “Extensions for GNU C++ compatibility” on page 416.
Extensions supporting the AltiVec Programming Interface
XL C/C++ supports AltiVec vector types when vector support is enabled. These
language extensions exploit the SIMD and parallel processing capabilities of the
® PowerPC processor, and facilitate the associated optimization techniques. The IBM
implementation of the AltiVec Programming Interface specification is an extended
implementation, which, for the most part, matches the syntax and semantics of the
GNU C implementation. In addition to the text provided throughout this
document which describes the behavior of the vector extensions, a list of the IBM
extensions to the AltiVec Programming Interface is also provided in “Extensions for
vector processing support” on page 411.
How this document is organized
This document is organized to loosely follow the structure of the ISO standard
language specifications and topics are grouped into similar headings.
Chapters 1 through 8 discuss language elements that are common to both C and
C++, including lexical elements, data types, declarations, declarators, type
conversions, expressions, operators, statements, and functions. Throughout these
chapters, both standard features and extensions are discussed. Chapters 9 through
16 discuss standard C++ features exclusively, including classes, overloading,
inheritance, templates, and exception handling. Chapter 17 discusses directives to
the preprocessor.
The appendices provide summary lists of all the extended features supported by
each language.
Conventions used in this document
Typographical conventions
The following table explains the typographical conventions used in this document.
Table 1. Typographical conventions
Typeface Indicates Example
bold Lowercase commands, executable If you specify -O3, the compiler
names, compiler options and assumes -qhot=level=0. To prevent
directives. all HOT optimizations with -O3, you
must specify -qnohot.
italics Parameters or variables whose Make sure that you update the size
actual names or values are to be parameter if you return more than
supplied by the user. Italics are the size requested.
also used to introduce new terms.
About this document ix
Table 1. Typographical conventions (continued)
Typeface Indicates Example
monospace Programming keywords and If one or two cases of a switch
library functions, compiler built-in statement are typically executed
functions, examples of program much more frequently than other
code, command strings, or cases, break out those cases by
user-defined names. handling them separately before the
switch statement.
Qualifying elements (icons and bracket separators)
This document uses marked bracket separators to delineate large blocks of text and
icons to delineate small segments of text as follows:
Table 2. Qualifying elements
Qualifier/Icon Meaning
C only The text describes a feature that is supported in the C language only;
C or describes behavior that is specific to the C language.
C++ only The text describes a feature that is supported in the C++ language
C++ only; or describes behavior that is specific to the C++ language.
IBM extension The text describes a feature that is an IBM XL C/C++ compiler
IBM extension to the standard language specifications.
Syntax diagrams
Throughout this document, diagrams illustrate XL C/C++ syntax. This section will
help you to interpret and use those diagrams.
v Read the syntax diagrams from left to right, from top to bottom, following the
path of the line.
The ─── symbol indicates the beginning of a command, directive, or statement.
The ─── symbol indicates that the command, directive, or statement syntax is
continued on the next line.
The ─── symbol indicates that a command, directive, or statement is continued
from the previous line.
The ─── symbol indicates the end of a command, directive, or statement.
Fragments, which are diagrams of syntactical units other than complete
commands, directives, or statements, start with the │─── symbol and end with
the ───│ symbol.
v Required items are shown on the horizontal line (the main path):