C++ Standard Template Library

From Minor Miracle Software
Revision as of 16:18, 26 June 2019 by WikiSysop (talk | contribs) (Concurrency)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

C++11 Standard Template Library


  • Source Material
The C++ Standard Library, 2nd Ed., Nicolai M. Josuttis. 2012

Introduction

Source Code.[1]

  • C++ Standards
  1. C++98 The first C++ standard approved in 1998. The official document is ISO/IEC 14882:1998.
  2. C++03 Bug fixes to C++98 approved in 2003. The official document is ISO/IEC 14882:2003.
  3. TR1 library extensions, std:tr1, approved in 2007. The official document is ISO/IEC TR 19768:2007.
  4. C++11[2] approved in 2011. The official document is ISO/IEC 14882:2011.
  5. C++14[3] approved in 2014.
  6. C++17[4]
  7. C++20[5]


  • Which C++ version is the compiler using?

Evaluate the predefined macro __cplusplus.

#define __cplusplus 201103L // C++11
#define __cplusplus 199711L // C++98 and C++03

Overview

New Language Features

p13 Chapter 3 New Language Features


  • Spaces in Template Expressions

The requirement to put a space between two closing template expressions has gone:

vector<list<int> >; // OK in each C++ version
vector<list<int>>; // OK since C++11
  • A real NULL Pointer

nullptr and std::nullptr_t

C++11 lets you use nullptr instead of 0 or NULL to specify that a pointer refers to no value (which
differs from having an undefined value). This new feature especially helps to avoid mistakes that
occurred when a null pointer was interpreted as an integral value. For example:
void f(int);
void f(void*);
f(0); // calls f(int)
f(NULL); // calls f(int) if NULL is 0, ambiguous otherwise
f(nullptr); // calls f(void*)
nullptr is a new keyword.

  • Automatic Type Deduction with auto

For example:

auto i = 42; // i has type int
double f();
auto d = f(); // d has type double
auto i; // ERROR: can’t deduce the type of i
static auto vat = 0.19; // Additional qualifiers are allowed.

General Concepts

Algorithms

Allocators

Concurrency

Threads


https://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-3.html https://rafalcieslak.wordpress.com/2014/05/16/c11-stdthreads-managed-by-a-designated-class/ https://docs.microsoft.com/en-us/cpp/parallel/multithreading-with-cpp-and-mfc?view=vs-2019 Creating threads, looks pretty good. https://thispointer.com/c-11-multithreading-part-1-three-different-ways-to-create-threads/

The C++ Standard Library A Tutorial and Reference (2nd Edition) pdf Concurrency - p945


Before C++11, there was no support for concurrency in the language and the C++ standard library, although implementations were free to give some guarantees.

thread_local - new keyword for defining variables wtih thread-specific values.

  • The library now provides support to start multiple threads, including passing arguments, return values, and exceptions across thread boundaries, as well as means to synchronize multiple threads, so we can synchronize both the control flow and data access.

Deeper into Concurrency
C++ Concurrency in Action by Anthony Williams.
Other concurrency experts Hans Boehm, Scott Meyers, Bartosz Milewski, Lawrence Crowl, and Peter Sommerlad. // sleep the current thread for milliseconds. this_thread::sleep_for(chrono::milliseconds(id(dre)));

Containers

Container Members in Detail

Special Containers

Function Objects and Lambdas

Internationalization

Iterators

Numerics

Regular Expressions

Stream Classes

Strings

Utilities

Chap. 2. p 7/36

Macros for different C++ standards
#define __cplusplus 199711L // C++98 and C++03
#define __cplusplus 201103L // C++11

Big O notation

Chap 3. p 13. New Features
Has a REAL null pointer, nullptr and std::nullptr_t. That evaluates to a true void*.
void f(int);
void f(void*);
f(0); // calls f(int)
f(NULL); // calls f(int) if NULL is 0, ambiguous otherwise
f(nullptr); // calls f(void*)

''auto'' lets you declare a variable without knowing it's type. For example,
auto i = 42;  // i has type int
double f();
auto d = f(); // d has type double
auto i;       // ERROR: can’t dedulce the type of i
This is useful if the return type is unknown or really long and complex.

Uniform Initialization and Initializer Lists
In earlier versions initializing could happen in many ways. The new approach uses curly braces.
int values[] { 1, 2, 3 };
std::vector<int> v { 2, 3, 5, 7, 11, 13, 17 };
std::vector<std::string> cities {"Berlin", "New York", "London", "Braunschweig", "Cairo", "Cologne"};
std::complex<double> c{4.0,3.0}; // equivalent to c(4.0,3.0)
That ''narrowing'' initializations reduce precision or where the supplied value gets modified— are not possible with braces. For example:
int x1(5.3); // OK, but OUCH: x1 becomes 5
int x2 = 5.3; // OK, but OUCH: x2 becomes 5
int x3{5.0}; // ERROR: narrowing
int x4 = {5.3}; // ERROR: narrowing
char c1{7}; // OK: even though 7 is an int, this is not narrowing
char c2{99999}; // ERROR: narrowing (if 99999 doesn’t fit into a char)
std::vector<int> v1 { 1, 2, 4, 5 }; // OK
std::vector<int> v2 { 1, 2.3, 4, 5.6 }; // ERROR: narrowing doubles to ints

The reserved word ''explicit'' prevents implicit type conversion.

Range-Based for Loops, the usual


3.1.5 Move Semantics and Rvalue References
Author says this is so complex I should read up on it elsewhere.

p. 19

p.23 3.1.6 New String Literals

p.24 Keyword noexcept, method will not throw an exception. This means if a subordinate method throws an exception the program will abort.

p.26 3.1.8 Keyword constexpr, "enable that expressions be evaluated at compile time."
Because an expression that resolves to an INT doesn't always work at compile time, but does at runtime.

3.1.9 New Template Features
* Variable argument list
* Alias Template

p.28 3.1.10 Lambdas introduced

typename - used to specify the following identifier is a 'type'. Very nice for pointers to class members.

p. 39 Chapter 4 General Concepts

p. 40 Name spaces

  • Header Files

No more extensions like .h, .hpp or .hxx. Instead use:

#include <iostream>
#include <string>






Internal Links

Parent Article: Main Page