Jerusalem .NET/C++ User Group• Bi-monthly meetings  – Alternating C++ and .NET content  – May have some mixed content as w...
// C++ 11             /*     Sasha Goldshtein      CTO, SELA Groupblog.sashag.net | @goldshtn             */
// The New C++/* * ISO Standard as of September ‘11 * Completely new C++ style   * Dozens of language features   * Dozens ...
// Automatic Type Inferenceauto x = 17;std::map<std::string, std::list<int>> m;auto it = m.begin(); //also: cbegin(), begi...
// Range-Based For Loopstd::vector<int> v;for (int n : v) {  std::cout << n << std::endl;}std::map<int, std::list<std::str...
// Decltypetemplate <typename T, typename U>auto add(const T& t, const U& u)  -> decltype(t+u) { return t + u; }template <...
// Lambda Functionsauto f1 = [](){};auto f2 = [](int n) { return n+1; };std::cout << f2(41);sort(begin(v), end(v), [](emp&...
// Higher-Order Functionsauto id = [](int n) { return n; };auto inc = [](const std::function<int(int)> f){   return [](int...
// Lambda Capturevoid foo() {  int x = 42;  auto f = [x]() { std::cout << x; };  f();  auto g = [&x]() { ++x; };  g();  st...
// Rvalue Referencesint x;x = 42;   //OK,     x is an lvalueint f();f() = 42; //NOT OK, f() is an rvalueint& g();g() = 42;...
// Rvalue Referencestemplate <typename T>void swap(T& a, T& b) {  T temp(a); a = b; b = temp;} //THREE deep copies are mad...
// Rvalue Referencesclass sstring {  char* data; int len;public:  sstring(const string& other) :    data(strdup(other.data...
// Rvalue Referencesstd::vector<sstring> v;for (int k = 0; k < 100; ++k) {  v.push_back(sstring(“Hello”));}//   How many t...
// Rvalue Referencesclass sstring { //Continuedpublic:  sstring(sstring&& other) {    data = other.data; other.data = null...
// Rvalue Referencesstd::vector<sstring> v;for (int k = 0; k < 100; ++k) {  v.push_back(sstring(“Hello”));}//   How many t...
// Smart Pointers/* * Three smart pointer classes   * No more new and delete * unique_ptr<T> - single ownership * shared_p...
// Threads and Asyncstd::thread t([]() { /*body*/ });void quicksort(int* a, int l, int r) {  int pivot = partition(a, l, r...
// Variadic Templatestemplate <typename Ts...>void print(Ts&&... ts) {}template <typename T, typename Ts...>void print(T&&...
// Summary: C++ Style/* *   Rely heavily on smart pointers *   Use lambdas when necessary *   Implement move semantics * *...
// Thanks!             /*      Sasha Goldshteinblog.sashag.net | @goldshtnPPT: s.sashag.net/jlmcppug1             */
Upcoming SlideShare
Loading in...5
×

C++11

4,871

Published on

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

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,871
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
78
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

C++11

  1. 1. 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
  2. 2. // C++ 11 /* Sasha Goldshtein CTO, SELA Groupblog.sashag.net | @goldshtn */
  3. 3. // 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*/
  4. 4. // Automatic Type Inferenceauto 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];
  5. 5. // Range-Based For Loopstd::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”);}
  6. 6. // Decltypetemplate <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;
  7. 7. // Lambda Functionsauto 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);});
  8. 8. // Higher-Order Functionsauto 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);
  9. 9. // Lambda Capturevoid 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;}
  10. 10. // Rvalue Referencesint 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?! */
  11. 11. // Rvalue Referencestemplate <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!
  12. 12. // Rvalue Referencesclass 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; }};
  13. 13. // Rvalue Referencesstd::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
  14. 14. // Rvalue Referencesclass sstring { //Continuedpublic: sstring(sstring&& other) { data = other.data; other.data = nullptr; len = other.len; } sstring& operator=(sstring&& other) { //similar }};
  15. 15. // Rvalue Referencesstd::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
  16. 16. // 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*/
  17. 17. // Threads and Asyncstd::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();}
  18. 18. // Variadic Templatestemplate <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”);
  19. 19. // Summary: C++ Style/* * Rely heavily on smart pointers * Use lambdas when necessary * Implement move semantics * * We only scratched the surface!*/
  20. 20. // Thanks! /* Sasha Goldshteinblog.sashag.net | @goldshtnPPT: s.sashag.net/jlmcppug1 */
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×