O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

C++ In a Nutshell

Book Description

To-the-point, authoritative, no-nonsense solutions have always been a trademark of O'Reilly books. The In a Nutshell books have earned a solid reputation in the field as the well-thumbed references that sit beside the knowledgeable developer's keyboard. C++ in a Nutshell lives up to the In a Nutshell promise. C++ in a Nutshell is a lean, focused reference that offers practical examples for the most important, most often used, aspects of C++.C++ in a Nutshell packs an enormous amount of information on C++ (and the many libraries used with it) in an indispensable quick reference for those who live in a deadline-driven world and need the facts but not the frills.The book's language reference is organized first by topic, followed by an alphabetical reference to the language's keywords, complete with syntax summaries and pointers to the topic references. The library reference is organized by header file, and each library chapter and class declaration presents the classes and types in alphabetical order, for easy lookup. Cross-references link related methods, classes, and other key features. This is an ideal resource for students as well as professional programmers.When you're programming, you need answers to questions about language syntax or parameters required by library routines quickly. What, for example, is the C++ syntax to define an alias for a namespace? Just how do you create and use an iterator to work with the contents of a standard library container? C++ in a Nutshell is a concise desktop reference that answers these questions, putting the full power of this flexible, adaptable (but somewhat difficult to master) language at every C++ programmer's fingertips.

Table of Contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. Structure of This Book
    2. About the Examples
    3. Conventions Used in This Book
    4. For More Information
    5. Comments and Questions
    6. Acknowledgments
  3. 1. Language Basics
    1. 1.1. Compilation Steps
    2. 1.2. Tokens
      1. 1.2.1. Identifiers
      2. 1.2.2. Keywords
      3. 1.2.3. Literals
        1. 1.2.3.1. Integer literals
        2. 1.2.3.2. Floating-point literals
        3. 1.2.3.3. Boolean literals
        4. 1.2.3.4. Character literals
        5. 1.2.3.5. String literals
      4. 1.2.4. Symbols
    3. 1.3. Comments
    4. 1.4. Character Sets
    5. 1.5. Alternative Tokens
    6. 1.6. Trigraphs
  4. 2. Declarations
    1. 2.1. Declarations and Definitions
      1. 2.1.1. Definitions
      2. 2.1.2. Ambiguity
    2. 2.2. Scope
    3. 2.3. Name Lookup
      1. 2.3.1. Qualified Name Lookup
      2. 2.3.2. Unqualified Name Lookup
      3. 2.3.3. Argument-Dependent Name Lookup
    4. 2.4. Linkage
      1. 2.4.1. Scope Linkage
      2. 2.4.2. Language Linkage
    5. 2.5. Type Declarations
      1. 2.5.1. Fundamental Types
      2. 2.5.2. Enumerated Types
      3. 2.5.3. POD Types
      4. 2.5.4. typedef Declarations
      5. 2.5.5. Elaborated Type Specifier
    6. 2.6. Object Declarations
      1. 2.6.1. Specifiers
        1. 2.6.1.1. Storage class specifiers
        2. 2.6.1.2. const and volatile qualifiers
        3. 2.6.1.3. Type specifiers
        4. 2.6.1.4. Using specifiers
      2. 2.6.2. Declarators
        1. 2.6.2.1. Arrays
        2. 2.6.2.2. Pointers
        3. 2.6.2.3. Function pointers
        4. 2.6.2.4. Member pointers
        5. 2.6.2.5. References
      3. 2.6.3. Initializers
        1. 2.6.3.1. Function-like initializers
        2. 2.6.3.2. Assignment-like initializers
        3. 2.6.3.3. Initializing arrays
        4. 2.6.3.4. Initializing scalars
      4. 2.6.4. Object Lifetime
    7. 2.7. Namespaces
      1. 2.7.1. Namespace Definitions
      2. 2.7.2. Namespace Aliases
      3. 2.7.3. using Declarations
      4. 2.7.4. using Directives
      5. 2.7.5. How to Use Namespaces
  5. 3. Expressions
    1. 3.1. Lvalues and Rvalues
    2. 3.2. Type Conversions
      1. 3.2.1. Arithmetic Types
      2. 3.2.2. Type Promotion
      3. 3.2.3. Arithmetic Type Conversions
      4. 3.2.4. Implicit Numeric Conversions
      5. 3.2.5. Lvalue Conversions
      6. 3.2.6. Conversion to bool
      7. 3.2.7. Type Casts
    3. 3.3. Constant Expressions
      1. 3.3.1. Integral Constant Expressions
      2. 3.3.2. Null Pointers
    4. 3.4. Expression Evaluation
      1. 3.4.1. Sequence Points
      2. 3.4.2. Order of Evaluation
      3. 3.4.3. Short-Circuit Evaluation
    5. 3.5. Expression Rules
      1. 3.5.1. Primary Expressions
      2. 3.5.2. Postfix Expressions
      3. 3.5.3. Unary Expressions
      4. 3.5.4. Cast Expressions
      5. 3.5.5. Pointer-to-Member Expressions
      6. 3.5.6. Multiplicative Expressions
      7. 3.5.7. Additive Expressions
      8. 3.5.8. Shift Expressions
      9. 3.5.9. Relational Expressions
      10. 3.5.10. Equality Expressions
      11. 3.5.11. Bitwise And Expressions
      12. 3.5.12. Bitwise Exclusive Or Expressions
      13. 3.5.13. Bitwise Inclusive Or Expressions
      14. 3.5.14. Logical And Expressions
      15. 3.5.15. Logical Or Expressions
      16. 3.5.16. Conditional Expressions
      17. 3.5.17. Assignment Expressions
      18. 3.5.18. Comma Expressions
  6. 4. Statements
    1. 4.1. Expression Statements
    2. 4.2. Declarations
      1. 4.2.1. Declaration Statements
      2. 4.2.2. Condition Declarations
    3. 4.3. Compound Statements
    4. 4.4. Selections
      1. 4.4.1. if Statements
      2. 4.4.2. switch Statements
    5. 4.5. Loops
      1. 4.5.1. while Statements
      2. 4.5.2. for Statements
      3. 4.5.3. do Statements
    6. 4.6. Control Statements
    7. 4.7. Handling Exceptions
  7. 5. Functions
    1. 5.1. Function Declarations
      1. 5.1.1. Return Type
      2. 5.1.2. Parameters
      3. 5.1.3. Default Arguments
      4. 5.1.4. Variable Number of Arguments
      5. 5.1.5. cv-qualifiers
      6. 5.1.6. Exception Specifications
    2. 5.2. Function Definitions
      1. 5.2.1. Declarations and Definitions
      2. 5.2.2. Function Types and Signatures
      3. 5.2.3. Function Bodies and try Blocks
    3. 5.3. Function Overloading
      1. 5.3.1. Declaring Overloaded Functions
      2. 5.3.2. Calling Overloaded Functions
      3. 5.3.3. Candidate Functions
        1. 5.3.3.1. Qualified member function call
        2. 5.3.3.2. Unqualified function call
        3. 5.3.3.3. Operator
        4. 5.3.3.4. Function-like initialization
        5. 5.3.3.5. Assignment-like initialization
        6. 5.3.3.6. Conversion initialization
        7. 5.3.3.7. Reference bound to conversion
      4. 5.3.4. Addresses of Overloaded Functions
      5. 5.3.5. Best Overloaded Function
        1. 5.3.5.1. Pruning the candidate list
        2. 5.3.5.2. Choosing the best function
        3. 5.3.5.3. Argument conversion sequences
    4. 5.4. Operator Overloading
      1. 5.4.1. Short-Circuit Operators
      2. 5.4.2. Comma Operator
      3. 5.4.3. Increment and Decrement
      4. 5.4.4. Member Reference
      5. 5.4.5. Function Call
      6. 5.4.6. operator new and operator delete
      7. 5.4.7. Type Conversion
    5. 5.5. The main Function
  8. 6. Classes
    1. 6.1. Class Definitions
      1. 6.1.1. Plain Old Data
      2. 6.1.2. Trivial Classes
      3. 6.1.3. Unions
      4. 6.1.4. Local Classes
    2. 6.2. Data Members
      1. 6.2.1. Data Layout
      2. 6.2.2. Mutable Data Members
      3. 6.2.3. Bit-Fields
      4. 6.2.4. Static Data Members
    3. 6.3. Member Functions
      1. 6.3.1. Pointers-to-Members
      2. 6.3.2. this Pointer
      3. 6.3.3. Static Member Functions
      4. 6.3.4. Constructors
        1. 6.3.4.1. Declaring constructors
        2. 6.3.4.2. Special constructors
        3. 6.3.4.3. Calling constructors
        4. 6.3.4.4. Throwing exceptions during construction
      5. 6.3.5. Destructors
      6. 6.3.6. Implicit Member Functions
        1. 6.3.6.1. Implicit default constructor
        2. 6.3.6.2. Implicit copy constructor
        3. 6.3.6.3. Implicit copy assignment operator
        4. 6.3.6.4. Implicit destructor
    4. 6.4. Inheritance
      1. 6.4.1. Virtual Functions
      2. 6.4.2. Covariant Return Types
      3. 6.4.3. Pure Virtual Functions
      4. 6.4.4. Abstract Classes
      5. 6.4.5. Multiple Inheritance
      6. 6.4.6. Virtual Inheritance
      7. 6.4.7. Polymorphic Constructors and Destructors
    5. 6.5. Access Specifiers
    6. 6.6. Friends
    7. 6.7. Nested Types
  9. 7. Templates
    1. 7.1. Overview of Templates
    2. 7.2. Template Declarations
    3. 7.3. Function Templates
      1. 7.3.1. Function Signatures
      2. 7.3.2. Deducing Argument Types
      3. 7.3.3. Overloading
      4. 7.3.4. Operators
    4. 7.4. Class Templates
      1. 7.4.1. Member Templates
      2. 7.4.2. Friends
    5. 7.5. Specialization
    6. 7.6. Partial Specialization
    7. 7.7. Instantiation
    8. 7.8. Name Lookup
      1. 7.8.1. Dependent Names
      2. 7.8.2. Resolving Names
      3. 7.8.3. Hiding Names
      4. 7.8.4. Using Type Names
    9. 7.9. Tricks with Templates
    10. 7.10. Compiling Templates
  10. 8. Standard Library
    1. 8.1. Overview of the Standard Library
    2. 8.2. C Library Wrappers
    3. 8.3. Wide and Multibyte Characters
      1. 8.3.1. Wide Characters
      2. 8.3.2. Multibyte Characters
      3. 8.3.3. Shift State
    4. 8.4. Traits and Policies
      1. 8.4.1. Character Traits
      2. 8.4.2. Iterator Traits
      3. 8.4.3. Custom Traits
    5. 8.5. Allocators
      1. 8.5.1. Using Allocators
      2. 8.5.2. Custom Allocators
    6. 8.6. Numerics
  11. 9. Input and Output
    1. 9.1. Introduction to I/O Streams
      1. 9.1.1. Custom I/O Operators
      2. 9.1.2. The C Standard I/O Library
      3. 9.1.3. C++ I/O Headers
    2. 9.2. Text I/O
      1. 9.2.1. Formatted Input
      2. 9.2.2. Formatted Output
    3. 9.3. Binary I/O
    4. 9.4. Stream Buffers
    5. 9.5. Manipulators
      1. 9.5.1. Standard Manipulators
      2. 9.5.2. Custom Manipulators
    6. 9.6. Errors and Exceptions
  12. 10. Containers, Iterators, and Algorithms
    1. 10.1. Containers
      1. 10.1.1. Standard Containers
      2. 10.1.2. Container Adapters
      3. 10.1.3. Pseudo-Containers
      4. 10.1.4. Container Requirements
        1. 10.1.4.1. Member types
        2. 10.1.4.2. Member functions
        3. 10.1.4.3. Exceptions
      5. 10.1.5. Using Containers
        1. 10.1.5.1. Value type requirements
        2. 10.1.5.2. Inserting values
        3. 10.1.5.3. Erasing values
        4. 10.1.5.4. Searching
    2. 10.2. Iterators
      1. 10.2.1. Iterator Categories
      2. 10.2.2. Iterator Safety
      3. 10.2.3. Special Iterators
      4. 10.2.4. Custom Iterators
      5. 10.2.5. const_iterators
      6. 10.2.6. Reverse Iterators
    3. 10.3. Algorithms
      1. 10.3.1. How Algorithms Work
      2. 10.3.2. Standard Algorithms
        1. 10.3.2.1. Nonmodifying operations
        2. 10.3.2.2. Comparison
        3. 10.3.2.3. Searching
        4. 10.3.2.4. Binary search
        5. 10.3.2.5. Modifying sequence operations
        6. 10.3.2.6. Sorting
        7. 10.3.2.7. Merging
        8. 10.3.2.8. Set operations
        9. 10.3.2.9. Heap operations
        10. 10.3.2.10. Permutations
      3. 10.3.3. Custom Algorithms
  13. 11. Preprocessor Reference
    1. # operator and directive — Stringify operator and null directive
    2. ## operator — Concatenation operator
    3. #define directive — Defines a macro
    4. defined operator — Tests whether a macro is defined
    5. #elif directive — Else-if for conditional compilation
    6. #else directive — Else for conditional compilation
    7. #endif directive — Ends conditional compilation
    8. #error directive — Issues an error message
    9. #if directive — Tests a condition
    10. #ifdef directive — Tests whether a macro is defined
    11. #ifndef directive — Tests whether a macro is undefined
    12. #include directive — Includes another source file
    13. #line directive — Changes the line number in error messages
    14. #pragma directive — Controls the compiler
    15. #undef directive — Undefines a macro
  14. 12. Language Reference
    1. and operator — Logical and operator
    2. and_eq operator — Bitwise and assignment operator
    3. asm definition — Inline assembler definition
    4. auto storage class — Automatic variable specifier
    5. bitand operator — Bitwise and operator
    6. bitor operator — Bitwise inclusive or operator
    7. bool type — Boolean (logical) type specifier
    8. break statement — Exits from a loop or switch statement
    9. case keyword — Case label for switch statement
    10. catch keyword — Exception handler in try statement
    11. char type — Character type specifier
    12. class keyword — Declares a class or template parameter, or elaborates a type name
    13. compl operator — Bitwise complement operator
    14. const qualifier — Marks objects and functions as constant
    15. const_cast operator — Cast to modify qualifiers
    16. continue statement — Reiterates a loop statement
    17. declaration — Function, namespace, object, type, template declaration
    18. declarator — Provides information about a single identifier in a declaration
    19. default keyword — Catch-all label in switch statement
    20. delete operator — Deletes a dynamic object or array of objects
    21. do statement — Test-at-bottom loop statement
    22. double type — Double-precision, floating-point type specifier
    23. dynamic_cast operator — Polymorphic cast of class type objects
    24. else keyword — Else part of if statement
    25. enum keyword — Declares enumerated type or elaborates an enumerated type name
    26. explicit specifier — Explicit constructor specifier
    27. export specifier — Exported template specifier
    28. expression — Any expression
    29. extern storage class — External storage class specifier
    30. false literal — Boolean false literal
    31. float type — Single-precision, floating-point type specifier
    32. for statement — For loop statement
    33. friend specifier — Grants access to private class members
    34. function — Function definition
    35. goto statement — Transfers execution to a labeled statement
    36. identifier — Name of an entity
    37. if statement — Selection statement
    38. inline specifier — Inline function specifier
    39. int type — Integer type specifier
    40. long type — Long type specifier
    41. mutable specifier — Mutable data member specifier
    42. namespace keyword — Declares namespace
    43. new operator — Allocates a dynamic object or array of objects
    44. not operator — Logical negation operator
    45. not_eq operator — Inequality operator
    46. operator keyword — Function call syntax for operators
    47. or operator — Logical or operator
    48. or_eq operator — Bitwise or assignment operator
    49. private access specifier — Restricts member access to the class
    50. protected access specifier — Restricts member access to the class and derived classes
    51. public access specifier — No restrictions on member access
    52. register storage class — Register storage class specifier
    53. reinterpret_cast operator — Cast for unsafe pointer conversions
    54. return statement — Returns from a function
    55. short type — Short integer type specifier
    56. signed specifier — Signed integer type specifier
    57. sizeof operator — Size of type operator
    58. statement — Statement syntax
    59. static storage class — Static storage class specifier
    60. static_cast operator — Explicit cast operator
    61. struct keyword — Declares class with public members
    62. switch statement — Multibranch selection statement
    63. template keyword — Declares a template, specialization, or instantiation
    64. this keyword — Object pointer in member function
    65. throw operator — Throws an exception
    66. true literal — Logical true literal
    67. try statement — Handles exceptions in statements
    68. type — Type specifiers
    69. typedef keyword — Declares a type synonym
    70. typeid operator — Runtime type identification operator
    71. typename keyword — Introduces a type name
    72. unsigned specifier — Unsigned integer type specifier
    73. union keyword — Declares a union
    74. using keyword — Looks up names in alternate classes or namespaces
    75. virtual specifier — Polymorphic function specifier or shared base class
    76. void keyword — Absence of type or function arguments
    77. volatile qualifier — Volatile qualifier
    78. wchar_t type — Wide-character type specifier
    79. while statement — Test-at-top unbounded loop statement
    80. xor operator — Bitwise exclusive or operator
    81. xor_eq operator — Bitwise exclusive or assignment operator
  15. 13. Library Reference
    1. 13.1. <algorithm>
    2. 13.2. <bitset>
    3. 13.3. <cassert>
    4. 13.4. <cctype>
    5. 13.5. <cerrno>
    6. 13.6. <cfloat>
    7. 13.7. <ciso646>
    8. 13.8. <climits>
    9. 13.9. <clocale>
    10. 13.10. <cmath>
    11. 13.11. <complex>
    12. 13.12. <csetjmp>
    13. 13.13. <csignal>
    14. 13.14. <cstdarg>
    15. 13.15. <cstddef>
    16. 13.16. <cstdio>
    17. 13.17. <cstdlib>
    18. 13.18. <cstring>
    19. 13.19. <ctime>
    20. 13.20. <cwchar>
    21. 13.21. <cwctype>
    22. 13.22. <deque>
    23. 13.23. <exception>
    24. 13.24. <fstream>
    25. 13.25. <functional>
    26. 13.26. <iomanip>
    27. 13.27. <ios>
    28. 13.28. <iosfwd>
    29. 13.29. <iostream>
    30. 13.30. <istream>
    31. 13.31. <iterator>
    32. 13.32. <limits>
    33. 13.33. <list>
    34. 13.34. <locale>
    35. 13.35. <map>
    36. 13.36. <memory>
    37. 13.37. <new>
    38. 13.38. <numeric>
    39. 13.39. <ostream>
    40. 13.40. <queue>
    41. 13.41. <set>
    42. 13.42. <sstream>
    43. 13.43. <stack>
    44. 13.44. <stdexcept>
    45. 13.45. <streambuf>
    46. 13.46. <string>
    47. 13.47. <strstream>
    48. 13.48. <typeinfo>
    49. 13.49. <utility>
    50. 13.50. <valarray>
    51. 13.51. <vector>
  16. A. Compiler Extensions
    1. A.1. Borland C++ Builder and Kylix
    2. A.2. GNU Compiler Collection
    3. A.3. Microsoft Visual C++
  17. B. Projects
    1. B.1. Blitz++
    2. B.2. Boost
    3. B.3. STLport
  18. Glossary
  19. About the Author
  20. Colophon
  21. Copyright