IBM XL C/C++ for Blue Gene/Q, V12.1
Language Reference
Ve r s i o n 12 .1
GC14-7364-00
IBM XL C/C++ for Blue Gene/Q, V12.1
Language Reference
Ve r s i o n 12 .1
GC14-7364-00 Note Before using this information and the product it supports, read the information in “Notices” on page 511.
First edition This edition applies to IBM XL C/C++ for Blue Gene/Q, V12.1 (Program 5799-AG1) 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 IBM Corporation 1998, 2012. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents
About this information ...... ix The register storage class specifier ...... 54 Who should read this information ...... ix The __thread storage class specifier (IBM How to use this information ...... ix extension) ...... 56 How this information is organized ...... ix Type specifiers ...... 58 Conventions ...... x Integral types...... 58 Related information ...... xiii Boolean types ...... 59 IBM XL C/C++ information ...... xiii floating-point types...... 59 Standards and specifications ...... xiv Character types ...... 60 Other IBM information ...... xv The void type ...... 61 Other information ...... xv Vector types (IBM extension) ...... 61 Technical support ...... xv User-defined types ...... 62 How to send your comments ...... xv The auto type specifier (C++0x) ...... 81 The decltype(expression) type specifier (C++0x) . 83 Chapter 1. Scope and linkage .....1 Compatibility of arithmetic types (C only) ....88 Type qualifiers ...... 89 Scope ...... 2 The __align type qualifier (IBM extension) . . . 90 Block/local scope ...... 2 The const type qualifier ...... 92 Function scope ...... 3 The restrict type qualifier ...... 93 Function prototype scope ...... 3 The volatile type qualifier ...... 94 File/global scope ...... 3 Type attributes (IBM extension)...... 95 Examples of scope in C ...... 4 The aligned type attribute ...... 96 Class scope (C++ only) ...... 5 The packed type attribute ...... 97 Namespaces of identifiers ...... 5 The transparent_union type attribute (C only) . . 97 Name hiding (C++ only) ...... 6 Program linkage ...... 7 Internal linkage ...... 8 Chapter 4. Declarators ...... 99 External linkage ...... 8 Overview of declarators ...... 99 No linkage ...... 9 Examples of declarators ...... 101 Language linkage (C++ only) ...... 9 Type names ...... 101 Pointers ...... 102 Chapter 2. Lexical Elements .....13 Pointer arithmetic ...... 103 Type-based aliasing ...... 104 Tokens ...... 13 Compatibility of pointers (C only) .....105 Keywords ...... 13 Arrays...... 106 Identifiers ...... 15 Variable length arrays ...... 107 Literals ...... 19 Compatibility of arrays ...... 108 Punctuators and operators ...... 32 References (C++ only) ...... 109 Source program character set ...... 34 Initializers ...... 110 Multibyte characters ...... 34 Initialization and storage classes ...... 111 Escape sequences ...... 35 Designated initializers for aggregate types (C The Unicode standard ...... 36 only) ...... 112 Digraph characters ...... 38 Initialization of vectors (IBM extension) . . . 114 Trigraph sequences ...... 39 Initialization of structures and unions ....115 Comments...... 39 Initialization of enumerations ...... 117 Initialization of pointers ...... 118 Chapter 3. Data objects and Initialization of arrays ...... 119 declarations ...... 43 Initialization of references (C++ only) ....121 Overview of data objects and declarations ....43 Initialization of complex types (C1X) ....125 Overview of data objects ...... 43 Variable attributes (IBM extension) ...... 126 Overview of data declarations and definitions . . 45 The aligned variable attribute ...... 127 _Static_assert declaration (C1X)...... 47 The common and nocommon variable attributes 128 static_assert declaration (C++0x) ...... 48 The init_priority variable attribute (C++ only) 129 Storage class specifiers...... 50 The mode variable attribute ...... 129 The auto storage class specifier ...... 51 The packed variable attribute ...... 130 The static storage class specifier ...... 51 The section variable attribute ...... 130 The extern storage class specifier ...... 53 The tls_model attribute ...... 131 The mutable storage class specifier (C++ only). . 54
© Copyright IBM Corp. 1998, 2012 iii The weak variable attribute...... 132 Pointer to member operators .* ->* (C++ only) 177 Conditional expressions ...... 178 Chapter 5. Type conversions .....133 Types in conditional C expressions (C only) . . 179 Arithmetic conversions and promotions ....134 Types in conditional C++ expressions (C++ Integral conversions ...... 134 only) ...... 179 Boolean conversions ...... 135 Examples of conditional expressions.....179 floating-point conversions ...... 135 Cast expressions ...... 180 Usual arithmetic conversions ...... 136 Cast operator ()...... 180 Integral and floating-point promotions ....138 The static_cast operator (C++ only) .....183 Lvalue-to-rvalue conversions ...... 140 The reinterpret_cast operator (C++ only) . . . 185 Pointer conversions ...... 141 The const_cast operator (C++ only) .....186 Conversion to void* ...... 142 The dynamic_cast operator (C++ only) ....188 Reference conversions (C++ only) ...... 142 Compound literal expressions ...... 191 Function argument conversions ...... 143 new expressions (C++ only) ...... 191 Placement syntax ...... 193 Chapter 6. Expressions and operators 145 Initialization of objects created with the new operator ...... 194 Lvalues and rvalues ...... 145 Handling new allocation failure ...... 194 Primary expressions ...... 147 delete expressions (C++ only) ...... 195 Names ...... 148 throw expressions (C++ only) ...... 196 Literals ...... 148 Label value expressions (IBM extension) ....196 Integer constant expressions ...... 148 Operator precedence and associativity .....197 Identifier expressions (C++ only) ...... 149 Reference collapsing (C++0x) ...... 201 Parenthesized expressions ( ) ...... 150 Scope resolution operator :: (C++ only)....151 Function call expressions ...... 152 Chapter 7. Statements ...... 205 Member expressions ...... 152 Labeled statements ...... 205 Dot operator ...... 153 Locally declared labels (IBM extension)....206 Arrow operator -> ...... 153 Labels as values (IBM extension) ...... 206 Unary expressions ...... 153 Expression statements ...... 207 Increment operator ++ ...... 154 Resolution of ambiguous statements (C++ only) 207 Decrement operator -- ...... 154 Block statements ...... 208 Unary plus operator + ...... 155 Example of blocks ...... 208 Unary minus operator - ...... 155 Statement expressions (IBM extension) ....209 Logical negation operator ! ...... 155 Selection statements ...... 209 Bitwise negation operator ~ ...... 156 The if statement ...... 209 Address operator & ...... 156 The switch statement ...... 211 Indirection operator *...... 157 Iteration statements ...... 215 The typeid operator (C++ only) ...... 158 The while statement ...... 215 The __alignof__ operator (IBM extension) . . . 159 The do statement ...... 216 The sizeof operator ...... 160 The for statement ...... 217 The typeof operator (IBM extension).....162 Jump statements ...... 219 The __real__ and __imag__ operators ....163 The break statement ...... 219 Binary expressions...... 164 The continue statement ...... 220 Assignment operators ...... 165 The return statement ...... 221 Multiplication operator * ...... 167 The goto statement ...... 223 Division operator / ...... 167 Null statement ...... 224 Remainder operator % ...... 167 Inline assembly statements (IBM extension) . . . 224 Addition operator + ...... 168 Supported and unsupported constructs....228 Subtraction operator - ...... 168 Restrictions on inline assembly statements. . . 229 Bitwise left and right shift operators << >> . . 168 Examples of inline assembly statements . . . 229 Relational operators<><=>= ...... 169 Equality and inequality operators == != . . . 170 Chapter 8. Functions ...... 235 Bitwise AND operator & ...... 171 Function declarations and definitions .....235 Bitwise exclusive OR operator ^ ...... 172 Function declarations...... 236 Bitwise inclusive OR operator | ...... 172 Function definitions ...... 236 Logical AND operator && ...... 173 Examples of function declarations .....237 Logical OR operator || ...... 173 Examples of function definitions ...... 238 Array subscripting operator [ ] ...... 174 Compatible functions (C only)...... 239 Vector subscripting operator [ ] (IBM extension) 175 Multiple function declarations (C++ only) . . . 239 Comma operator , ...... 176 Function storage class specifiers ...... 240 iv XL C/C++: Language Reference The static storage class specifier ...... 240 Overloading binary operators ...... 293 The extern storage class specifier ...... 241 Overloading assignments ...... 294 Function specifiers ...... 242 Overloading function calls ...... 295 The inline function specifier ...... 242 Overloading subscripting ...... 296 The _Noreturn function specifier ...... 247 Overloading class member access...... 297 Function return type specifiers ...... 247 Overload resolution ...... 298 Function return values ...... 248 Implicit conversion sequences ...... 299 Function declarators ...... 249 Resolving addresses of overloaded functions 305 Parameter declarations ...... 250 Trailing return type (C++0x) ...... 252 Chapter 11. Classes (C++ only) ....307 Function attributes (IBM extension) ...... 255 Declaring class types ...... 307 The alias function attribute ...... 257 Using class objects...... 308 The always_inline function attribute.....257 Classes and structures...... 310 The const function attribute ...... 258 Scope of class names ...... 311 The constructor and destructor function Incomplete class declarations ...... 312 attributes ...... 258 Nested classes ...... 312 The format function attribute ...... 258 Local classes...... 314 The format_arg function attribute .....259 Local type names ...... 315 gnu_inline ...... 260 malloc...... 261 Chapter 12. Class members and The noinline function attribute ...... 262 The noreturn function attribute ...... 262 friends (C++ only) ...... 317 The pure function attribute ...... 262 Class member lists ...... 317 The section function attribute ...... 263 Data members ...... 318 used ...... 263 Member functions ...... 319 The weak function attribute ...... 263 Inline member functions...... 319 weakref ...... 264 Constant and volatile member functions . . . 320 The main() function ...... 265 Virtual member functions ...... 320 Function calls ...... 266 Special member functions ...... 321 Pass by value ...... 267 Member scope ...... 321 Pass by pointer ...... 268 Pointers to members ...... 322 Pass by reference (C++ only) ...... 268 The this pointer ...... 324 Allocation and deallocation functions (C++ only) 270 Static members ...... 327 Default arguments in C++ functions (C++ only) 271 Using the class access operators with static Restrictions on default arguments (C++ only) 272 members ...... 327 Evaluation of default arguments (C++ only) . . 273 Static data members ...... 328 Pointers to functions ...... 274 Static member functions ...... 330 Nested functions (IBM extension)...... 275 Member access ...... 331 Friends ...... 334 Chapter 9. Namespaces (C++ only) 277 Friend scope...... 338 Friend access ...... 340 Defining namespaces ...... 277 Declaring namespaces ...... 277 Creating a namespace alias ...... 277 Chapter 13. Inheritance (C++ only) 341 Creating an alias for a nested namespace ....278 Derivation ...... 343 Extending namespaces ...... 278 Inherited member access ...... 346 Namespaces and overloading ...... 279 Protected members ...... 346 Unnamed namespaces ...... 279 Access control of base class members ....347 Namespace member definitions ...... 281 The using declaration and class members ....348 Namespaces and friends...... 281 Overloading member functions from base and The using directive ...... 282 derived classes ...... 349 The using declaration and namespaces .....283 Changing the access of a class member ....351 Explicit access ...... 283 Multiple inheritance ...... 352 Inline namespace definitions (C++0x) .....284 Virtual base classes ...... 353 Multiple access ...... 354 Chapter 10. Overloading (C++ only) 287 Ambiguous base classes ...... 355 Virtual functions ...... 359 Overloading functions ...... 287 Ambiguous virtual function calls ...... 363 Restrictions on overloaded functions ....288 Virtual function access ...... 364 Overloading operators ...... 289 Abstract classes...... 365 Overloading unary operators ...... 291 Overloading increment and decrement operators 292
Contents v Chapter 14. Special member functions throw expressions ...... 452 (C++ only) ...... 367 Rethrowing an exception ...... 452 Overview of constructors and destructors ....367 Stack unwinding ...... 454 Constructors...... 368 Exception specifications ...... 456 Default constructors ...... 369 Special exception handling functions .....459 Delegating constructors (C++0x) ...... 370 The unexpected() function ...... 459 Explicit initialization with constructors ....372 The terminate() function...... 460 Initialization of base classes and members . . . 373 The set_unexpected() and set_terminate() Constructors execution order of class objects 377 functions ...... 461 Destructors ...... 379 Example using the exception handling functions 462 Pseudo-destructors ...... 381 User-defined conversions ...... 381 Chapter 17. Preprocessor directives 465 Conversion constructors ...... 383 Macro definition directives ...... 465 Explicit conversion constructors ...... 384 The #define directive ...... 466 Conversion functions ...... 385 The #undef directive ...... 470 Explicit conversion operators (C++0x) ....386 The # operator ...... 471 Copy constructors ...... 387 The ## operator ...... 472 Copy assignment operators...... 389 Standard predefined macro names .....473 File inclusion directives ...... 474 Chapter 15. Templates (C++ only). . . 391 The #include directive ...... 474 Template parameters ...... 392 The #include_next directive (IBM extension) . . 476 Type template parameters ...... 392 Conditional compilation directives ...... 477 Non-type template parameters ...... 392 The #if and #elif directives ...... 478 Template template parameters ...... 393 The #ifdef directive ...... 479 Default arguments for template parameters . . 393 The #ifndef directive ...... 479 Naming template parameters as friends (C++0x) 394 The #else directive...... 479 Template arguments ...... 395 The #endif directive ...... 480 Template type arguments ...... 395 Message generation directives ...... 481 Template non-type arguments ...... 396 The #error directive ...... 481 Template template arguments ...... 398 The #warning directive (IBM extension) . . . 481 Class templates...... 399 The #line directive...... 482 Class template declarations and definitions . . 401 Assertion directives (IBM extension)...... 483 Static data members and templates .....402 Predefined assertions ...... 484 Member functions of class templates ....403 The null directive (#) ...... 484 Friends and templates ...... 403 Pragma directives ...... 484 Function templates ...... 404 The _Pragma preprocessing operator ....485 Template argument deduction ...... 406 Standard pragmas (C only) ...... 485 Overloading function templates ...... 412 C99 preprocessor features adopted in C++0x . . . 486 Partial ordering of function templates ....413 Template instantiation ...... 414 Chapter 18. The IBM XL C language Explicit instantiation ...... 414 extensions ...... 491 Implicit instantiation ...... 417 General IBM extensions ...... 491 Template specialization ...... 418 C99 features ...... 491 Explicit specialization ...... 419 Extensions for C1X compatibility ...... 493 Partial specialization ...... 423 Extensions for GNU C compatibility...... 494 Variadic templates (C++0x) ...... 426 Extensions for Unicode support ...... 496 Name binding and dependent names .....438 Extensions for vector processing support ....496 The typename keyword ...... 440 Extension of #endif ...... 497 The template keyword as qualifier ...... 440 Chapter 19. The IBM XL C++ language Chapter 16. Exception handling (C++ extensions ...... 499 only) ...... 443 General IBM extensions ...... 499 try blocks ...... 443 Extensions for C99 compatibility ...... 499 Nested try blocks ...... 444 Extensions for Unicode support ...... 500 catch blocks ...... 445 Extensions for C++0x compatibility ...... 501 Function try block handlers ...... 446 Extensions for GNU C compatibility...... 503 Arguments of catch blocks ...... 450 Extensions for GNU C++ compatibility .....504 Matching between exceptions thrown and Extensions for vector processing support ....505 caught...... 450 Extension of #endif ...... 506 Order of catching ...... 451 Extending the lifetime of C++ temporaries . . . 506 vi XL C/C++: Language Reference Binding an rvalue to a non-const or volatile lvalue Trademarks and service marks ...... 513 reference ...... 510 Index ...... 515 Notices ...... 511
Contents vii viii XL C/C++: Language Reference About this information
This information describes the syntax, semantics, and IBM® XL C/C++ Advanced Edition for Linux 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 information This information 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 information to find 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 information 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 information covers both standard and implementation-specific features, it does not include the following topics: v Standard C and C++ library functions and headers. For information on the standard C and C++ libraries, refer to your operating system information. v Constructs for writing multi-threaded programs, including OpenMP directives and functions and POSIX Pthread functions. For reference information on OpenMP constructs, see the XL C/C++ Compiler Reference; for information on Pthreads library functions, refer to your Blue Gene®/Q information. v Compiler pragmas, predefined macros, and built-in functions. These are described in the XL C/C++ Compiler Reference.
How this information is organized
This information is organized to loosely follow the structure of the ISO standard language specifications and topics are grouped into similar headings. v 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. v Chapters 9 through 16 discuss standard C++ features exclusively, including classes, overloading, inheritance, templates, and exception handling. v Chapter 17 discusses directives to the preprocessor.
© Copyright IBM Corp. 1998, 2012 ix v Chapters 18 and 19 provide summary lists of all the extended features supported by each language.
Conventions Typographical conventions
The following table shows the typographical conventions used in the IBM XL C/C++ for Blue Gene/Q, V12.1 information. Table 1. Typographical conventions Typeface Indicates Example bold Lowercase commands, executable The compiler provides basic names, compiler options, and invocation commands, bgxlc and directives. bgxlC (bgxlc++), along with several other compiler invocation commands to support various C/C++ language levels and compilation environments. 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. underlining The default setting of a parameter nomaf | maf of a compiler option or directive. monospace Programming keywords and To compile and optimize library functions, compiler builtins, myprogram.c, enter: bgxlc examples of program code, myprogram.c -O3. command strings, or user-defined names.
Qualifying elements (icons)
Most features described in this information apply to both C and C++ languages. In descriptions of language elements where a feature is exclusive to one language, or where functionality differs between languages, this information uses icons to delineate segments of text as follows: Table 2. Qualifying elements Qualifier/Icon Meaning C only, or C only begins The text describes a feature that is supported in the C language C only; or describes behavior that is specific to the C language.
C
C only ends C++ only, or C++ only The text describes a feature that is supported in the C++ begins language only; or describes behavior that is specific to the C++ C++ language.
C++
C++ only ends
x XL C/C++: Language Reference Table 2. Qualifying elements (continued) Qualifier/Icon Meaning IBM extension begins The text describes a feature that is an IBM extension to the IBM standard language specifications.
IBM
IBM extension ends C1X, or C1X begins The text describes a feature that is introduced into standard C C1X as part of C1X.
C1X
C1X ends C++0x, or C++0x begins The text describes a feature that is introduced into standard C++0x C++ as part of C++0x.
Cz/OS++0x
C++0x ends
Syntax diagrams
Throughout this information, 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):