C++11, BS book extract, part II, chapter 12 + 13

Parts of a Function Declaration

  • [[noreturn]], indicating that the function will not return using the normal call/return mechanism
  • override, indicating that it must be overriding a virtual function from a base class
  • final, indicating that it cannot be overriden in a derived class

That is, a prefix auto indicates that the return type is placed after the argument list. The suffix return type is preceded by −>.
The essential use for a suffix return type comes in function template declarations in which the return type depends on the arguments. For example:
template<class T, class U> auto product(const vector<T>& x, const vector<U>& y) −> decltype(x∗y);
However, the suffix return syntax can be used for any function.

A constexpr function cannot have side effects, so writing to nonlocal objects is not possible.

If there is a possible ambiguity, an initializer_list parameter takes priority.

Unspecified Number of Arguments

  • a variadic template
  • an initializer_list as the argument type
  • Terminate the argument list with the ellipsis ( … )

 

Automatic Overload Resolution

  • Exact match
  • Match using promotions
  • Match using standard conversions
  • Match using user-defined conversions
  • Match using the ellipsis … in a function declaration

 

Overloading takes place among the members of an overload set. By default, that means the functions of a single scope; functions declared in different non-namespace scopes do not overload.

 

The C++ standard library provides provides one of the following guarantees for every library operation:

  • The basic guarantee for all operations: The basic invariants of all objects are maintained, and no resources, such as memory, are leaked.
  • The strong guarantee for key operations: in addition to providing the basic guarantee, either the operation succeeds, or it has no effect.
  • The nothrow guarantee for some operations: in addition to providing the basic guarantee, some operations are guaranteed not to throw an exception.

 

Both the basic guarantee and the strong guarantee are provided on the condition that

  • user-supplied operations (such as assignments and swap() functions) do not leave container elements in invalid states
  • user-supplied operations do not leak resources
  • destructors do not throw exceptions

 

double compute(double) noexcept; // may not throw an exception. It terminates unconditionally by invoking std::terminate() if exception is thrown.

 

The noexcept Operator – template<typename T> void my_fct(T& x) noexcept(Is_pod<T>());

The handler is invoked:

  • [1] If H is the same type as E
  • [2] If H is an unambiguous public base of E
  • [3] If H and E are pointer types and [1] or [2] holds for the types to which they refer
  • [4] If H is a reference and [1] or [2] holds for the type to which H refers

 

The guiding principles are:

  • Don’t throw an exception while handling an exception.
  • Don’t throw an exception that can’t be caught.

 

The specific rules for calling terminate() are

• When no suitable handler was found for a thrown exception
• When a noexcept function tries to exit with a throw
• When a destructor invoked during stack unwinding tries to exit with a throw
• When code invoked to propagate an exception (e.g., a copy constructor) tries to exit with a throw
• When someone tries to rethrow ( throw; ) when there is no current exception being handled
• When a destructor for a statically allocated or thread-local object tries to exit with a throw
• When an initializer for a statically allocated or thread-local object tries to exit with a throw
• When a function invoked as an atexit() function tries to exit with a throw

 

By default, terminate() will call abort(). If that is not acceptable, the user can provide a terminate handler function by a call std::set_terminate() from <exception>

If an exception is not caught on a thread, std::terminate() is called.

 

Advertisements

C++11, BS book extract, part II, chapter 7 + 8 +9 + 10 + 11

nullptr instead of NULL

raw string literalsR”(ccc)” notation for a sequence of characters ccc, for wchar_t – it is LR”(angst)”, u8 – UTF8, u – UTF16,  U – UTF32, can be combined with R as well.

Reference to reference can only happen as the result of an alias  or a template type argument.

 Unqualified Lists – {} are used besides initialization a variable

  • A function argument
  • A return value
  • The right-hand operand of an assignment operator ( = , += , ∗= , etc.)
  • A subscript

 

A lambda expression consists of a sequence of parts:

  • A possibly empty capture list
  • An optional parameter list
  • An optional mutable specifier
  • An optional noexcept specifier
  • An optional return type declaration of the form −> type
  • A body, specifying the code to be executed. The body is delimited by {}

Lambda capture list can be –

  • [] : an empty capture list (no captures).
  • [&] : implicitly capture by reference (all local names)
  • [=] : implicitly capture by value (all local names)
  • [ capture-list ] : explicit capture
  • [&, capture-list ] : implicitly capture by reference all local variables with names not mentioned in the list
  • [=, capture-list ] : implicitly capture by value all local variables with names not mentioned in the list

A lambda that captures nothing can be assigned to a pointer to function of an appropriate type

C++11, BS book extract, part II, chapter 6

Fundamental Types

  • bool
  • (signed/unsigned)char, wchar_t, char16_t (UTF16), char32_t(UTF32)
  • short, (signed/unsigned) int, long long
  • float, double, long double
  • void
  • pointer
  • array
  • reference
  • struct, class
  • enum

{} -initializer syntax to prevent narrowing alignof() operator returns the alignment of its argument expression we can use the type specifier alignas : alignas(T) means ‘‘align just like a T .’’ void user(const vector& vx) {     constexpr int bufmax = 1024;     alignas(X) buffer[bufmax];     // uninitialized     const int max = min(vx.size(),bufmax/siz eof(X));     uninitialized_copy(vx.begin(),vx.begin()+max,buffer);     // … }   Scope

  • Local scope
  • Class scope
  • Namespace scope
  • Global scope
  • Statement scope
  • Function scope

Initialization X a1 {v}; – initializer list – prevents data gets lost, like narrowing (integer of float point datatypes) or floating integer type conversion but auto should be initialized with assignment auto z1 {99}; // z1 is an initializer_list auto z2 = 99; // z2 is an int difference for vector as well vector v1 {99}; // v1 is a vector of 1 element with the value 99 vector v2(99); // v2 is a vector of 99 elements each with the default value 0  Prefer {} initialization over alternatives unless you have a strong reason not to. The empty initializer list, {} , is used to indicate that a default value is desired. Nice example – char buf[max] {}; // initialize every char to 0 If initialization is missed

  • Static objects (global, local static, static members) are initialized by default
  • Dynamic – heap or stack based – are not initialized.

The language provides two mechanisms for deducing a type from an expression:

  • auto for deducing a type of an object from its initializer; the type can be the type of a vari- able, a const , or a constexpr .
  • decltype(expr) for deducing the type of something that is not a simple initializer, such as the return type for a function or the type of a class member.

Weird syntax template auto operator+(const Matrix& a, const Matrix& b) −> Matrix<decltype(T{}+U{})>;   The lifetime of an object starts when its constructor completes and ends when its destructor starts executing.

  • Automatic
  • Static
  • Free store (Heap)
  • Temporary objects
  • Thread-local objects; that is, objects declared thread_local

using instead of typedef, using could not be prepended with unsigned/signed

C++11, BS book extract, part I, chapter 5

Concurrency, task,  threads.

Sharing data – mutex, unique_lock

condition_variable, methods – wait,  notify_one, …

Tasks – promise (set_value, set_exception), future (get), packaged_task, async…

Type functions, like numeric limits

Tuple – like pair, but for unlimited amount of types/members

Regexp

Math, complex numbers, random numbers

Valarray – vector arithmetic