Namespaces
Variants
Views
Actions

C++ language

From cppreference.com
< cpp
Revision as of 14:46, 7 March 2013 by 69.84.133.248 (Talk)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

This is a brief reference of available C++ language constructs.

Contents

General topics

Preprocessor

Comments

Keywords

ASCII chart

Escape sequences

History of C++

Basic definitions

Flow control

Conditional execution statements

Conditional statements execute different code paths according to the value of given expression.

  • if executes code conditionally
  • switch executes code according to the value of an integral argument

Iteration statements

Iteration statements execute a code path multiple times.

  • for executes loops by specifying initialization, comparison, and increment
  • range-for executes loops over a range (since C++11)
  • while executes loop, checking condition before each iteration
  • do-while executes loop, checking condition after each iteration

Jump statements

Jump statements continue program execution at a different location.

  • continue skips the remaining part of the enclosing loop body
  • break terminates the enclosing loop
  • goto continues execution in another location
  • return terminates execution of the enclosing function

Functions

The same code can be reused at different locations in the program.

Exceptions

Exceptions are a more robust way to signal error condition than function return codes or global error variables.

Namespaces

Namespaces provide a way to prevent name clashes in large projects.

Types

  • fundamental types define basic character, integer and floating point types
  • pointer types define types holding a memory location
  • compound types define types that hold several data members (essentially the same as class)
  • enumeration types define types that are able to hold only one of the specified values
  • union types define types that can hold data in several representations
  • function types define function call signatures, that is the types of arguments and the return type
  • decltype specifier defines a type equivalent to the type of an expression (since C++11)

Specifiers

Initialization

Whenever a named variable is declared, and whenever a temporary object is created, the initial value of the new object is provided through one of the following mechanisms:

Literals

Literals are the tokens of a C++ program that represent constant values, embedded in the source code.

Expressions

An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects.

  • value categories (lvalue, rvalue, glvalue, prvalue, xvalue) classify expressions by their values
  • order of evaluation of arguments and subexpressions specify the order in which intermediate results are obtained
  • operators allow the use of syntax commonly found in mathematics
Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

Special operators

static_cast converts one type to another compatible type
dynamic_cast converts virtual base class to derived class
const_cast converts type to compatible type with different cv qualifiers
reinterpret_cast converts type to incompatible type
new allocates memory
delete deallocates memory
sizeof queries the size of a type
sizeof... queries the size of a parameter pack (since C++11)
typeid queries the type information of a type
noexcept checks if an expression can throw an exception (since C++11)
alignof queries alignment requirements of a type (since C++11)

Utilities

Types
Casts
Memory allocation

Classes

Classes provide the concept of object-oriented programming in C++.

Class-specific function properties

Special member functions

Templates

Allows functions and classes to operate on generic types

Start and termination

Optimizations

  • the as-if rule allows any code transformation that doesn't change the output
  • copy elision, including RVO and NRVO, makes pass-by-value the preferred approach in many situations.
  • empty base optimization makes multiple inheritance from interfaces or policy classes overhead-free and is required for standard-layout types.

Miscellaneous