SlideShare a Scribd company logo
1 of 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

             /*
     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
             */

More Related Content

What's hot

C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
mspline
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 

What's hot (20)

C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
C++11
C++11C++11
C++11
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
C++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect ForwardingC++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect Forwarding
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
C++ references
C++ referencesC++ references
C++ references
 
C++ 11
C++ 11C++ 11
C++ 11
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 

Viewers also liked

Object Oriented Program
Object Oriented ProgramObject Oriented Program
Object Oriented Program
Alisha Jain
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 

Viewers also liked (20)

Les nouveautés de C++11 : Ecrire du C++ Moderne
Les nouveautés de C++11 : Ecrire du C++ ModerneLes nouveautés de C++11 : Ecrire du C++ Moderne
Les nouveautés de C++11 : Ecrire du C++ Moderne
 
What's New in C++ 11/14?
What's New in C++ 11/14?What's New in C++ 11/14?
What's New in C++ 11/14?
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Object Oriented Program
Object Oriented ProgramObject Oriented Program
Object Oriented Program
 
C++ arrays part2
C++ arrays part2C++ arrays part2
C++ arrays part2
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointers
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
C++ classes
C++ classesC++ classes
C++ classes
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 

Similar to C++11

C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
Vivek Das
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
LeonardN9WWelchw
 
Practical Meta Programming
Practical Meta ProgrammingPractical Meta Programming
Practical Meta Programming
Reggie Meisler
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
afgt2012
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
forladies
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
SyedHaroonShah4
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
Yandex
 

Similar to C++11 (20)

C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ application
 
The STL
The STLThe STL
The STL
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11
 
The Rust Programming Language: an Overview
The Rust Programming Language: an OverviewThe Rust Programming Language: an Overview
The Rust Programming Language: an Overview
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
Practical Meta Programming
Practical Meta ProgrammingPractical Meta Programming
Practical Meta Programming
 
About Go
About GoAbout Go
About Go
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methods
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 

More from Sasha Goldshtein

The Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF PrimerThe Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF Primer
Sasha Goldshtein
 
Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF Abyss
Sasha Goldshtein
 

More from Sasha Goldshtein (20)

Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
 
The Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF PrimerThe Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF Primer
 
Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF Abyss
 
Visual Studio 2015 and the Next .NET Framework
Visual Studio 2015 and the Next .NET FrameworkVisual Studio 2015 and the Next .NET Framework
Visual Studio 2015 and the Next .NET Framework
 
Swift: Apple's New Programming Language for iOS and OS X
Swift: Apple's New Programming Language for iOS and OS XSwift: Apple's New Programming Language for iOS and OS X
Swift: Apple's New Programming Language for iOS and OS X
 
C# Everywhere: Cross-Platform Mobile Apps with Xamarin
C# Everywhere: Cross-Platform Mobile Apps with XamarinC# Everywhere: Cross-Platform Mobile Apps with Xamarin
C# Everywhere: Cross-Platform Mobile Apps with Xamarin
 
Modern Backends for Mobile Apps
Modern Backends for Mobile AppsModern Backends for Mobile Apps
Modern Backends for Mobile Apps
 
.NET Debugging Workshop
.NET Debugging Workshop.NET Debugging Workshop
.NET Debugging Workshop
 
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
 
Mastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and ProductionMastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and Production
 
Introduction to RavenDB
Introduction to RavenDBIntroduction to RavenDB
Introduction to RavenDB
 
State of the Platforms
State of the PlatformsState of the Platforms
State of the Platforms
 
Delivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in MinutesDelivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in Minutes
 
Building Mobile Apps with a Mobile Services .NET Backend
Building Mobile Apps with a Mobile Services .NET BackendBuilding Mobile Apps with a Mobile Services .NET Backend
Building Mobile Apps with a Mobile Services .NET Backend
 
Building iOS and Android Apps with Mobile Services
Building iOS and Android Apps with Mobile ServicesBuilding iOS and Android Apps with Mobile Services
Building iOS and Android Apps with Mobile Services
 
Task and Data Parallelism
Task and Data ParallelismTask and Data Parallelism
Task and Data Parallelism
 
Attacking Web Applications
Attacking Web ApplicationsAttacking Web Applications
Attacking Web Applications
 
Windows Azure Mobile Services
Windows Azure Mobile ServicesWindows Azure Mobile Services
Windows Azure Mobile Services
 
First Steps in Android Development
First Steps in Android DevelopmentFirst Steps in Android Development
First Steps in Android Development
 
First Steps in iOS Development
First Steps in iOS DevelopmentFirst Steps in iOS Development
First Steps in iOS Development
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

C++11

  • 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 Group blog.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 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];
  • 5. // 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”); }
  • 6. // 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;
  • 7. // 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); });
  • 8. // 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);
  • 9. // 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; }
  • 10. // 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?! */
  • 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 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; } };
  • 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 class sstring { //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 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(); }
  • 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 */