The reader should be forewarned that extensive knowledge of the C programming language is actually assumed. The C++ Annotations continue where topics of the C programming language end, such as pointers, basic flow control and the construction of functions.
Some elements of the language, like specific lexical tokens (like
digraphs (e.g.,
<: for
[, and
>: for
])) are not covered
by the C++ Annotations, as these tokens occur extremely seldom in C++ source
code. In addition, trigraphs (using
??< for
{, and
??> for
}) have been removed from C++.
The working draft of the C++ standard is freely available, and can be cloned from the git-repository at
The version number of the C++ Annotations (currently 11.6.0) is updated when the content
University of Groningen,
PO Box 407,
9700 AK Groningen
The Netherlands
In this chapter an overview of C++'s defining features is presented. A few extensions to C are reviewed and the concepts of object based and object oriented programming (OOP) are briefly introduced.
std::optionalclass is used to return values from functions that are optionally available,
std::iotais used to fill a range with a sequence of incremented values.
C++2aconcepts.
C++2astandard.
error_codes, error_categories,and
error_conditions(cf. sections 4.3.2, 10.9, and 23.7).
C++2astandard. Version 11.0.0 is made available at the time the
C++2astandard has not yet formally been released, and compilers do not yet implement all its new elements. Therefore, modifications and updates may be required once the
C++2astandard has officially become the next standard. However, the elements of the new standard that are now covered by the C++ Annotations are (mostly) supported by compilers.
std::filesystemnamespace received a new subsection showing which facilities of the
std::filesystemnamespace are replacing traditional C functions.
std::tupleelements in combination with structured binding declarations. content was modified and further improved (thanks to countless readers who sent us their comments). argument,.
Always use the latest C++ standard supported by your compiler. When the
latest standard isn't used by default, but is already partially implemented it
can usually be selected by specifying the appropriate flag. E.g., to use the
C++2a standard specify the flag
-
-std=c++2a. In the
C++ Annotations it is assumed that this flag is used when compiling the
examples.
g++compiler (see also).
The GNU
g++ compiler's official home page is, also containing
information about how to install the compiler in an MS-Windows system.
source.cc':
g++ source.cc
This produces a binary program (
a.out or
a.exe). If the default
name is inappropriate, the name of the executable can be specified using the
-o flag (here producing the program
source):
g++ -o source source.cc
If a mere compilation is required, the compiled module can be produced using
the
-c flag:
g++ -c source.cc
This generates the file
source.o, which can later on be linked to
other modules.++. Traditionally (and still encountered with some popular object oriented languages) more complex types are thereupon derived from the basic type, with each derived type adding some new functionality. From these derived types, more complex types can again be derived ad infinitum, until a representation of the entire problem can be made.
Over the years this approach has become less popular in C++ as it typically results in very tight coupling among those types, which in turns reduces rather than enhances the understanding, maintainability and testability of complex programs. The term coupling refers to the degree of independence between software components: tight coupling means a strong dependency, which is frowned upon in C++. In C++ object oriented programs more and more favor small, easy to understand hierarchies, limited coupling and a developmental process where design patterns (cf. Gamma et al. (1995)) play a central role. elaborated until
#endif pair; }
Implicit conversions from
void * to non-void pointers are not
allowed. E.g., the following isn't accepted in C++:
void *none() { return 0; } int main() { int *empty = none(); }are defined, only differing in their parameter lists, expecting an
int,
double content which only differ in their
return types would therefore not be distinguishable to the compiler.
constmember functions is introduced (cf. section 7.7). Here it is merely mentioned that classes normally have so-called member functions associated with them (see, e.g., chapter 5 for an informal introduction. In such cases only one of these functions must have the
constattribute.
: it is the compiler who supplies the lacking argument unless it is explicitly specified at the call. The code of the program will neither be shorter nor more efficient when default arguments are used. void two_ints(int a = 1, int b = 4); // code of function in, say, two.cc void two_ints(int a, int b) { ... }
It is an error to supply default arguments in both function definitions and function declarations. When applicable default arguments should be provided in function declarations: when the function is used by other sources the compiler commonly reads the header file rather than the function definition itself. Consequently the compiler has no way to determine the values of default arguments if they are provided in the function definition. with any set of arguments. In C the keyword
void is used when
it is the explicit intent to declare a function with no arguments at all, as
in:
void func(void);
As C++ enforces strict type checking, in C++ an empty parameter
list indicates the total absence of parameters. The keyword
void is
thus omitted.
_(int which(int size); #ifdef __cplusplus } #endif
Using this setup, a normal C header file is enclosed by
extern "C"
{ which occurs near the top of the file and by
}, which occurs near
the bottom of the file. The
#ifdef directives
When this file is initially scanned by the preprocessor, the symbol
MYHEADER_H_ is not yet defined. The
#ifndef condition succeeds and all
declarations are scanned. In addition, the symbol
MYHEADER_H_ is defined.
When this file is scanned next while compiling the same source file,.
In addition, starting with the
C++2a standard modules are available
resulting in a somewhat more efficient way of handling declarations than
offered by the traditional header files. In the C++ Annotations modules are
covered in chapter 7, section 7.12.statement. This implies that `
c' is available only to the
switchstatement itself, including its nested (sub)statements, but not outside the scope of the
switch.
The same approach can be used with
if and
while statements: a
variable that is defined in the condition clause statements, or
localize the definition of
int c using or other compound type is defined, the tag of
this type can be used as type name (this is
SomeStruct.
andand
or, not defined. C++ changed this for postfix expressions, assignment expressions (including compound assignments), and shift operators:
variable++++does not compile)).
In the following examples
first is evaluated before
second, before
third, before
fourth, whether they are single variables, parenthesized
expressions, or function calls:
first.second fourth += third = second += first first << second << third << fourth first >> second >> third >> fourth
In addition, when overloading an operator, the function implementing the overloaded operator is evaluated like the built-in operator it overloads, and not in the way function calls are generally ordered.