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

             /*
     Sasha Goldshtein
      CTO, SELA Group
blog.sashag.net | @goldshtn
             */
// 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
*/
// 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];
// 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”);
}
// 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;
// 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);
});
// 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);
// 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;
}
// Rvalue References
int x;
x = 42;   //OK,     x is an lvalue
int f();
f() = 42; //NOT OK, f() is an rvalue
int& g();
g() = 42; //OK,     g() is an lvalue

/* An rvalue reference is a reference
   to an rvalue, written as && ...
   WHY?!                                */
// 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!
// 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;
  }
};
// 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
// Rvalue References
class sstring { //Continued
public:
  sstring(sstring&& other) {
    data = other.data; other.data = nullptr;
    len = other.len;
  }
  sstring& operator=(sstring&& other) {
    //similar
  }
};
// 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
// 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
*/
// 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();
}
// 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”);
// Summary: C++ Style
/*
 *   Rely heavily on smart pointers
 *   Use lambdas when necessary
 *   Implement move semantics
 *
 *   We only scratched the surface!
*/
// Thanks!

             /*
      Sasha Goldshtein
blog.sashag.net | @goldshtn
PPT: s.sashag.net/jlmcppug1
             */

C++11

  • 1.
    Jerusalem .NET/C++ UserGroup • 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 Group blog.sashag.net | @goldshtn */
  • 3.
    // The NewC++ /* * 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 TypeInference 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];
  • 5.
    // Range-Based ForLoop 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”); }
  • 6.
    // Decltype template <typenameT, 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 Functions autof1 = [](){}; 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 Functions autoid = [](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 Capture voidfoo() { 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 References intx; x = 42; //OK, x is an lvalue int f(); f() = 42; //NOT OK, f() is an rvalue int& g(); g() = 42; //OK, g() is an lvalue /* An rvalue reference is a reference to an rvalue, written as && ... WHY?! */
  • 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!
  • 12.
    // Rvalue References classsstring { 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 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
  • 14.
    // Rvalue References classsstring { //Continued public: sstring(sstring&& other) { data = other.data; other.data = nullptr; len = other.len; } sstring& operator=(sstring&& other) { //similar } };
  • 15.
    // 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
  • 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 andAsync 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(); }
  • 18.
    // 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”);
  • 19.
    // Summary: C++Style /* * Rely heavily on smart pointers * Use lambdas when necessary * Implement move semantics * * We only scratched the surface! */
  • 20.
    // Thanks! /* Sasha Goldshtein blog.sashag.net | @goldshtn PPT: s.sashag.net/jlmcppug1 */