20
Jerusalem .NET/C++ User Group • Bi-monthly meetings – Alternating C++ and .NET content – May have some mixed content as well • Please participate! • Sponsors: BrightSource, SELA Group

C++11

Embed Size (px)

DESCRIPTION

A brief presentation on the C++11 standard, including lambda functions, rvalue references, automatic variables, and more.

Citation preview

Page 1: C++11

Jerusalem .NET/C++ User Group

• Bi-monthly meetings– Alternating C++ and .NET content–May have some mixed content as well

• Please participate!

• Sponsors: BrightSource, SELA Group

Page 2: C++11

// C++ 11

/*Sasha GoldshteinCTO, SELA Group

blog.sashag.net | @goldshtn */

Page 3: C++11

// The New C++

/* * ISO Standard as of September ‘11 * Completely new C++ style * Dozens of language features * Dozens of STL features * GCC has full support (exp.) * VS10 – partial, VS11 – more*/

Page 4: C++11

// Automatic Type Inference

auto x = 17;

std::map<std::string, std::list<int>> m;auto it = m.begin(); //also: cbegin(), begin(m)

auto f = std::bind(std::less<float>, _1, 3.0f);

int arr[100];const auto& s = &arr[42];

Page 5: C++11

// Range-Based For Loop

std::vector<int> v;for (int n : v) { std::cout << n << std::endl;}

std::map<int, std::list<std::string>> m;for (auto& p : m) { p.second.push_back(“Winter is coming”);}

Page 6: C++11

// Decltype

template <typename T, typename U>auto add(const T& t, const U& u) -> decltype(t+u) { return t + u; }

template <typename T>void foo() { decltype(something(T().bar()) l;}std::pair<int, float> p;decltype(p.second) f = p.second; f += 0.1f;decltype((p.second)) rf = p.second; rf += 0.1f;

Page 7: C++11

// Lambda Functions

auto f1 = [](){};auto f2 = [](int n) { return n+1; };std::cout << f2(41);

sort(begin(v), end(v), [](emp& e1, emp& e2) { return e1.bonus > e2.bonus;});parallel_foreach(begin(v), end(v), [](emp& e) { salary_module::calculate_salary(e);});

Page 8: C++11

// Higher-Order Functions

auto id = [](int n) { return n; };auto inc = [](const std::function<int(int)> f) { return [](int n) { return f(n) + 1; }};

auto f = id;f = inc(f); std::cout << f(1);f = inc(f); std::cout << f(1);

Page 9: C++11

// Lambda Capture

void foo() { int x = 42; auto f = [x]() { std::cout << x; }; f(); auto g = [&x]() { ++x; }; g(); std::cout << x; auto h = [x]() mutable { ++x; }; h(); std::cout << x;}

Page 10: C++11

// Rvalue References

int x;x = 42; //OK, x is an lvalueint f();f() = 42; //NOT OK, f() is an rvalueint& g();g() = 42; //OK, g() is an lvalue

/* An rvalue reference is a reference to an rvalue, written as && ... WHY?! */

Page 11: C++11

// Rvalue References

template <typename T>void swap(T& a, T& b) { T temp(a); a = b; b = temp;} //THREE deep copies are made!

template <typename T>void swap(T& a, T& b) { T temp(a); //Assuming T has move ctor a = std::move(b); b = std::move(temp);} //ZERO deep copies are made!

Page 12: C++11

// Rvalue References

class sstring { char* data; int len;public: sstring(const string& other) : data(strdup(other.data), len(other.len) {} sstring& operator=(const string& other) if (this == &other) return *this; free(data); data = strdup(other.data); len = other.len; }};

Page 13: C++11

// Rvalue References

std::vector<sstring> v;for (int k = 0; k < 100; ++k) { v.push_back(sstring(“Hello”));}

// How many times “Hello” is allocated?// Answer: 200// How many times “Hello” is freed?// Answer: 100

Page 14: C++11

// Rvalue References

class sstring { //Continuedpublic: sstring(sstring&& other) { data = other.data; other.data = nullptr; len = other.len; } sstring& operator=(sstring&& other) { //similar }};

Page 15: C++11

// Rvalue References

std::vector<sstring> v;for (int k = 0; k < 100; ++k) { v.push_back(sstring(“Hello”));}

// How many times “Hello” is allocated?// Answer: 100// How many times “Hello” is freed?// Answer: 0

Page 16: C++11

// Smart Pointers

/* * Three smart pointer classes * No more new and delete * unique_ptr<T> - single ownership * shared_ptr<T> - shared ownership * weak_ptr<T> - cycle breaking*/

Page 17: C++11

// Threads and Async

std::thread t([]() { /*body*/ });

void quicksort(int* a, int l, int r) { int pivot = partition(a, l, r); auto fl = std::async([&]() { quicksort(a, l, pivot); }); auto f2 = std::async([&]() { quicksort(a, pivot+1, r); }); f1.wait(); f2.wait();}

Page 18: C++11

// Variadic Templates

template <typename Ts...>void print(Ts&&... ts) {}

template <typename T, typename Ts...>void print(T&& t, Ts&&... ts) { std::cout << t << std::endl; print(ts...);}

print(4, 2.5f, “Hello”);

Page 19: C++11

// Summary: C++ Style

/* * Rely heavily on smart pointers * Use lambdas when necessary * Implement move semantics * * We only scratched the surface!*/

Page 20: C++11

// Thanks!

/*Sasha Goldshteinblog.sashag.net |

@goldshtnPPT:

s.sashag.net/jlmcppug1*/