C++11
Upcoming SlideShare
Loading in...5
×
 

C++11

on

  • 5,040 views

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

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

Statistics

Views

Total Views
5,040
Views on SlideShare
2,143
Embed Views
2,897

Actions

Likes
4
Downloads
47
Comments
0

11 Embeds 2,897

http://blogs.microsoft.co.il 2770
http://feeds.feedburner.com 87
http://beta.blogs.microsoft.co.il 13
http://www.selagroup.com 10
http://subscribe.ru 5
http://guideme.co.il 4
http://scc.sela.co.il 3
http://reader.googleusercontent.com 2
http://search.mywebsearch.com 1
http://translate.googleusercontent.com 1
http://webcache.googleusercontent.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

C++11 Presentation Transcript

  • 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. // C++ 11 /* Sasha Goldshtein CTO, SELA Groupblog.sashag.net | @goldshtn */
  • 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. // 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. // 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. // 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. // 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. // 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. // 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. // 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. // 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. // 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. // 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. // Rvalue Referencesclass sstring { //Continuedpublic: sstring(sstring&& other) { data = other.data; other.data = nullptr; len = other.len; } sstring& operator=(sstring&& other) { //similar }};
  • 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. // 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. // 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. // 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. // Summary: C++ Style/* * Rely heavily on smart pointers * Use lambdas when necessary * Implement move semantics * * We only scratched the surface!*/
  • 20. // Thanks! /* Sasha Goldshteinblog.sashag.net | @goldshtnPPT: s.sashag.net/jlmcppug1 */