Created by
Henrique Ferrolho / @ferrolho
João Pereira / @joaofpereira
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}; ```
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 ```
C++ 03 ```c++ vector<int> vec; vector<int>::iterator itr = vec.iterator(); ``` C++ 11 ```c++ vector<int> vec; auto itr = vec.iterator(); ```
less typing == less typos
=> IDE plays a more important role
C++ 03 ```c++ int multiply(int x, int y); ``` C++ 11 ```c++ auto multiply(int x, int y) -> int; ```
Why is this any good..?
C++ 11 ```c++ int x = 3; decltype(x) y = x; // same thing as `auto y = x;` ```
```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! ```
C++ 11 ```c++ /* * This function takes only nullptr, and nothing else. */ void func(std::nullptr_t); ```
Stronger types
Increased safety
is meant to make COMPILATION fail
is meant to end the execution of your program. (abort)
Used in places that require compile-time evaluation
```cpp
template
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() }; ```
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 ```
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
```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; ```
lvalue
rvalue
```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; } ```
- Learn C++ 11 in 20 Minutes - Part I - Part II
- C++ 11: Rvalue Reference - Move Semantics
- Alex Allain C++ 11 tutorial series