C++ 11

A tutorial about the new features

Created by
Henrique Ferrolho / @ferrolho
João Pereira / @joaofpereira

Table of contents

C++

general-purpose programming language

multi-paradigm

imperative
object-oriented
generic

initializer_list

C++ 03 ```c++ int arr[5] = {1, 2, 3, 4, 5}; vector<int> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5); ``` C++ 11 ```c++ int arr[5] = {1, 2, 3, 4, 5}; vector<int> vec = {1, 2, 3, 4, 5}; ```

auto type

C++ 03 ```c++ int x = 4; double y = 3.14; int z = x; ``` C++ 11 ```c++ auto x = 4; // x is an integer auto y = 3.14; // y is a double auto z = x; // z is an integer ```

auto type

C++ 03 ```c++ vector<int> vec; vector<int>::iterator itr = vec.iterator(); ``` C++ 11 ```c++ vector<int> vec; auto itr = vec.iterator(); ```

auto type

less typing == less typos

=> IDE plays a more important role

new return value syntax

C++ 03 ```c++ int multiply(int x, int y); ``` C++ 11 ```c++ auto multiply(int x, int y) -> int; ```


Why is this any good..?

decltype

declared type inspection

C++ 11 ```c++ int x = 3; decltype(x) y = x; // same thing as `auto y = x;` ```

nullptr

```c++ #define NULL 0 ``` C++ 03 ```c++ void func(int n); void func(char* s); func(NULL); // calls `func(int)`... ``` C++ 11 ```c++ void func(int n); void func(char* s); func(nullptr); // calls `func(char*)` - yay! ```

nullptr

std::nullptr_t - a new unique type

C++ 11 ```c++ /* * This function takes only nullptr, and nothing else. */ void func(std::nullptr_t); ```

enum class

to wrap it up

Stronger types

Increased safety

static_assert

what's different ?

static_assert

is meant to make COMPILATION fail

assert

is meant to end the execution of your program. (abort)

constexpr

Used in places that require compile-time evaluation

template parameters
array-size specifiers

example

```cpp template class fixed_size_list { /*...*/ }; fixed_size_list mylist; // X must be an integer constant expression int numbers[X]; // X must be an integer constant expression ```

final - delete - override

final

used in classes and virtual functions

class ```cpp class Dog final { // No class can be derived from Dog /* ... */ }; ```

virtual function ```cpp class Dog { virtual void bark() final; // No class can override bark() }; ```

delete

C++03 ```cpp class Person { public: Person(int age); }; Person joao(2); Person henrique(4.0); // 4.0 is converted from double to int joao = henrique; // the compiler generates an assignment operator ```

override

C++03 ```cpp class Old { virtual void A(int i); virtual void B() const; }; class New : public Old{ virtual void A(float i); // Created a new function virtual void B(); // Created a new function }; ```

mistakes happen

lambda functions

(aka) anonymous functions

```c++ /* * lambda syntax: * * [capture](parameters) -> return_type { body } * * *parameters*, and *return_type* are optional */ [] {} // valid lambda function ``` ```c++ cout << [](int x, int y) { return x + y; } (3, 4) << endl; // or auto func = [](int x, int y) { return x + y; }; cout << func(3, 4) << endl; ```

rvalue reference

understanding lvalue and rvalue

lvalue

  • persist beyond a single expression
  • objects that have a name
  • all variables, including const variables, are lvalues.

rvalue

  • temporary values
  • do not persist beyond the expression that uses them

rvalue reference

function overloading

```c++ void printInt(int& i) { cout << "lvalue reference: " << i << endl; } void printInt(int&& i) { cout << "rvalue reference: " << i << endl; } ``` ```c++ int a = 4; printInt(a); // calls printInt(int& i) printInt(6); // calls printInt(int&& i) ``` Warning! ```c++ /* * Adding this function would cause a compilation error. * The compiler wouldn't know which function to call for both calls. */ void printInt(int i) { cout << i << endl; } ```

THE END

- Learn C++ 11 in 20 Minutes - Part I - Part II
- C++ 11: Rvalue Reference - Move Semantics
- Alex Allain C++ 11 tutorial series