SlideShare a Scribd company logo
Gentle* Introduction to
Modern C++
Mihai Todor
* Well, maybe not so gentle 
Disclaimer: This presentation is focused on C++11 features and assumes a basic understanding of C++
Intro I
 C++ is boring / hard to get right
 The C++ compilers are buggy
 We don’t want to mess with it
Intro II
And your code looks like this*:
*That’s actually C# code, but you get the point 
Intro III
WRONG MENTALITY!
Intro IV
In spite of the fact that it lacks some of
the C# flexibility and compactness,
C++ is a beautiful language, having a
rich syntax, which enables a high
degree of expressivity and abstraction,
with a fine grained control over
resource consumption.
Intro V
 If done right, in C++ you only pay for
what you use
 If done wrong, in C++ you blow your
entire leg off, not just a toe
Intro VI
 The compiler is never* wrong
 C++11 features alleviate much of the
pain we had to endure when
architecting code
 The STL is becoming richer and more
flexible
 Compilers are adopting new features
much faster
*well, maybe sometimes, but it usually requires more than 10 minutes of coding to stumble upon a legit compiler bug
Some modern C++ features
 Move semantics
 Auto
 nullptr
 Range-based for loops
 override and final
 Strongly-typed enums
 Lambdas
 Explicitly defaulted and deleted functions
 Smart pointers
Move semantics
 Copies are expensive!
 Want speed, pass by value (not always*)
 Moving variables can be as cheap as a
pointer swap
 Classes can now have move constructors
and move assignment operators
* http://juanchopanzacpp.wordpress.com/2014/05/11/want-speed-dont-always-pass-by-value/
Move semantics II
 In C++11, an expression can be:
The && operator has been introduced in order to declare an rvalue reference
Diagram shamelessly ripped from here: http://stackoverflow.com/a/3601748/1174378
Move semantics III
 xvalue (an “eXpiring” value) – also refers to
an object, usually near the end of its lifetime
 A type Type followed by && represents an rvalue
reference
 xvalues are explicit moves, created by std::move
 prvalue (“pure” rvalue) - an rvalue that is not
an xvalue (implicit moves)
Move semantics IV
 A reference type that is declared using
& is called an lvalue reference
 A reference type that is declared using
&& is called an rvalue reference.
Move semantics V
 Powerful tools in the <utility> header:
 std::move
 std::forward
 std::make_move_iterator
A thorough explanation for std::move and std::forward:
http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx
auto
 Repurposed keyword (initially, it designated a
storage duration specifier)
 Similar to C#’s var keyword
 Reduces code verbosity by deducing a variable’s
type from the initializer
 Also allows return type deduction (C++14)
 Potential for abuse!
nullptr
 Eliminates the ambiguous conversion between NULL
(#defined as 0) and integral types
 Denotes a value of type std::nullptr_t
 Converts implicitly to bool (as false) and null pointer values of
any type
 You don’t have to test a variable for null with both NULL and
nullptr!
int *pointer = LegacyGetInt();
if (pointer != nullptr && pointer != NULL)//both tests are equivalent
Range-based for loops
 They resemble the C# foreach statement
 Syntax: for (auto value: collection) {/* do stuff */}
 the type specifier of value determines how the collection
items are iterated: by value, by reference, by const
reference, etc
 Can be used with any type for which non-member
overloads of the begin() and end() functions are
implemented
 see NonMemberBeginAndEnd.cpp
override and final
 If override is added to the declaration of a virtual
method, which does not override a virtual method
from the base class, the compiler emits an error
 If final is added to the declaration of a virtual
method, then derived classes are not allowed to
override this method
 in C++, the virtual specifier is optional for derived class
override methods
 If final is added to the declaration of a class, then
no other classes can derive from that class. This
resembles C#’s sealed keyword
Strongly-typed enums
 Eliminate issues caused by regular enums: namespace
pollution and implicit conversions
 For now, there is no simple way to use these in the same way
as a C# flags enum (tagged enumeration)
 The enum class underlying type can be specified by the user:
enum class Colors : std::int8_t { RED = 1, GREEN = 2, BLUE = 3 };
 Extracting underlying values of an enum class requires an
explicit cast:
auto blue = static_cast<std::underlying_type<Colors>::type>(Colors::Blue);
Smart pointers
 They provide reference counting in order to enable the automatic
release of memory
 Concerns about performance degradation / extra memory
consumption are, indeed, legitimate, but in most cases we are not
creating millions of objects on the heap to have really tight
constraints
 std::unique_ptr – Guarantees unique ownership of a memory
resource. It cannot be copied (only moved to another
std::unique_ptr).
 Created with std::make_unique (C++14)
 std::auto_ptr is obsolete! Please replace it on sight with
std::unique_ptr.
Smart pointers II
 std::shared_ptr – Allows shared ownership of a
memory resource
 Created with std::make_shared (C++11)
 std::weak_ptr – Holds a reference to an object
managed by an std::shared_ptr.
 does not contribute to the reference count
 Ensures that cycles are broken
 std::static_pointer_cast, std::dynamic_pointer_cast
and std::const_pointer_cast are your friends!
Lambdas
 C++11 introduces support for anonymous
functions, namely lambdas
 Fine-grained control over variable capture
 By default, variables captured by value are
const, unless the mutable specification is
present
 We can have recursive lambdas
Lambdas II
 Syntax (shamelessly ripped from MSDN*)
1. lambda-introducer (Also known as the capture clause)
2. lambda declarator (Also known as the parameter list).
Optional!
3. mutable (Also known as the mutable specification) .
Optional!
4. exception-specification (Also known as the exception
specification) . Optional!
5. trailing-return-type (Also known as the return type) .
Optional!
6. compound-statement (Also known as the lambda body)
* http://msdn.microsoft.com/en-us/library/dd293603.aspx
Explicitly defaulted and
deleted functions
 The rules which specify when the implicitly-
declared default constructor and destructor,
copy and move constructors and copy and
move assignment operators get generated
are somewhat complex, so C++11 allows
you to force the compiler to generate them
or not, via the =default and =delete
specifiers.
 Please take the Rules of three/five/zero into
account when using these specifiers!
Explicitly defaulted and
deleted functions II
 Let’s design a trivial object, which can be
constructed and moved, but not copied
class UniqueObject
{
public:
UniqueObject() =default;
~UniqueObject() =default;
UniqueObject(const UniqueObject &obj) =delete;
UniqueObject &operator=(const UniqueObject &obj) =delete;
UniqueObject(UniqueObject &&obj) =default;
UniqueObject &operator=(UniqueObject &&obj) =default;
};
More modern C++ features?
 static_assert and type traits
 Mutable
 Constructor delegation
 http://www.nullptr.me/2012/01/17/c11-delegating-constructors/
 Uniform initialization
 http://programmers.stackexchange.com/questions/133688/is-c11-
uniform-initialization-a-replacement-for-the-old-style-syntax
 rvalue reference for *this
 http://stackoverflow.com/questions/12306226/what-does-an-ampersand-
after-this-assignment-operator-mean
 Many more…
Follow me on:
Twitter: @MihaiTodor
Linkedin: www.linkedin.com/mtodor
See my activity on StackOverflow:
http://stackoverflow.com/users/1174378/mihai-todor

More Related Content

PDF
Modern C++
Michael Clark
 
PPTX
C++11: Feel the New Language
mspline
 
PPTX
C++ 11 Features
Jan Rüegg
 
PPTX
C++11
Quang Trần Duy
 
PDF
C++11 & C++14
CyberPlusIndia
 
PPTX
C++11
Andrey Dankevich
 
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Francesco Casalegno
 
PDF
Modern c++ (C++ 11/14)
Geeks Anonymes
 
Modern C++
Michael Clark
 
C++11: Feel the New Language
mspline
 
C++ 11 Features
Jan Rüegg
 
C++11 & C++14
CyberPlusIndia
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Francesco Casalegno
 
Modern c++ (C++ 11/14)
Geeks Anonymes
 

What's hot (20)

PPT
What's New in C++ 11?
Sasha Goldshtein
 
PDF
C++11: Rvalue References, Move Semantics, Perfect Forwarding
Francesco Casalegno
 
PPTX
C++11
Sasha Goldshtein
 
PPTX
C++ Presentation
Carson Wilber
 
PPTX
C++ 11
Denys Haryachyy
 
PDF
Smart Pointers in C++
Francesco Casalegno
 
PDF
C++17 introduction - Meetup @EtixLabs
Stephane Gleizes
 
PPTX
The Style of C++ 11
Sasha Goldshtein
 
PDF
Regular types in C++
Ilio Catallo
 
PPTX
Summary of C++17 features
Bartlomiej Filipek
 
PPTX
Smart pointers
Vishal Mahajan
 
PDF
Fun with Lambdas: C++14 Style (part 1)
Sumant Tambe
 
PDF
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Chris Adamson
 
PDF
Design Patterns in Modern C++
Dmitri Nesteruk
 
PDF
C++ Training
SubhendraBasu5
 
PPTX
Fun with Lambdas: C++14 Style (part 2)
Sumant Tambe
 
PDF
Modern C++ Explained: Move Semantics (Feb 2018)
Olve Maudal
 
PPT
STL ALGORITHMS
fawzmasood
 
PDF
(5) cpp dynamic memory_arrays_and_c-strings
Nico Ludwig
 
PDF
C++ references
corehard_by
 
What's New in C++ 11?
Sasha Goldshtein
 
C++11: Rvalue References, Move Semantics, Perfect Forwarding
Francesco Casalegno
 
C++ Presentation
Carson Wilber
 
Smart Pointers in C++
Francesco Casalegno
 
C++17 introduction - Meetup @EtixLabs
Stephane Gleizes
 
The Style of C++ 11
Sasha Goldshtein
 
Regular types in C++
Ilio Catallo
 
Summary of C++17 features
Bartlomiej Filipek
 
Smart pointers
Vishal Mahajan
 
Fun with Lambdas: C++14 Style (part 1)
Sumant Tambe
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Chris Adamson
 
Design Patterns in Modern C++
Dmitri Nesteruk
 
C++ Training
SubhendraBasu5
 
Fun with Lambdas: C++14 Style (part 2)
Sumant Tambe
 
Modern C++ Explained: Move Semantics (Feb 2018)
Olve Maudal
 
STL ALGORITHMS
fawzmasood
 
(5) cpp dynamic memory_arrays_and_c-strings
Nico Ludwig
 
C++ references
corehard_by
 
Ad

Viewers also liked (20)

PPTX
Fion's Image Gallery
tse_fionkl
 
PPTX
Destination Dog Pitch
ashley_willis
 
PDF
NPS Q1 report
Adriana Bozbiciu
 
PDF
Specific questions Adriana Bozbiciu
Adriana Bozbiciu
 
PPTX
Fion's major assignment image gallery
tse_fionkl
 
PPTX
Fion's Image Gallery
tse_fionkl
 
PPT
Raport q4
Adriana Bozbiciu
 
PPSX
Up ftour11
Italia_UPF
 
PPTX
Fion's Image Gallery
tse_fionkl
 
PDF
General questions Adriana Bozbiciu
Adriana Bozbiciu
 
PDF
Raport Q1 AIESEC Romania
Adriana Bozbiciu
 
PDF
Minimal Introduction to C++ - Part I
Michel Alves
 
PPT
2 Intro c++
Docent Education
 
PDF
Minimal Introduction to C++ - Part II
Michel Alves
 
PDF
Minimal Introduction to C++ - Part III - Final
Michel Alves
 
PPTX
Aiesec romania data report pre q1
Adriana Bozbiciu
 
PDF
DNB-Final PMR Question papers 2010 to 2013
Amit Ranjan
 
PPT
C++ Introduction
parmsidhu
 
PPT
Chapter 01 - introduction for C++
wahida_f6
 
PDF
An Introduction to C++ A complete beginners guide - Michael Oliver
cttvl
 
Fion's Image Gallery
tse_fionkl
 
Destination Dog Pitch
ashley_willis
 
NPS Q1 report
Adriana Bozbiciu
 
Specific questions Adriana Bozbiciu
Adriana Bozbiciu
 
Fion's major assignment image gallery
tse_fionkl
 
Fion's Image Gallery
tse_fionkl
 
Raport q4
Adriana Bozbiciu
 
Up ftour11
Italia_UPF
 
Fion's Image Gallery
tse_fionkl
 
General questions Adriana Bozbiciu
Adriana Bozbiciu
 
Raport Q1 AIESEC Romania
Adriana Bozbiciu
 
Minimal Introduction to C++ - Part I
Michel Alves
 
2 Intro c++
Docent Education
 
Minimal Introduction to C++ - Part II
Michel Alves
 
Minimal Introduction to C++ - Part III - Final
Michel Alves
 
Aiesec romania data report pre q1
Adriana Bozbiciu
 
DNB-Final PMR Question papers 2010 to 2013
Amit Ranjan
 
C++ Introduction
parmsidhu
 
Chapter 01 - introduction for C++
wahida_f6
 
An Introduction to C++ A complete beginners guide - Michael Oliver
cttvl
 
Ad

Similar to Gentle introduction to modern C++ (20)

PPT
LLVM
guest3e5046
 
PDF
67404923-C-Programming-Tutorials-Doc.pdf
Rajb54
 
PPTX
C_plus_plus
Ralph Weber
 
PPTX
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
PDF
C++ Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
DOCX
C tutorials
Amit Kapoor
 
PDF
Object Oriented Programming using C++ PCIT102.pdf
GauravKumar295392
 
PPTX
C++ language
Hamza Asif
 
PPT
C# features
sagaroceanic11
 
PDF
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Rohit Singh
 
PPTX
C++ programming language basic to advance level
sajjad ali khan
 
PDF
fundamental of c++ for students of b.tech iii rd year student
Somesh Kumar
 
PPTX
Gude for C++11 in Apache Traffic Server
Apache Traffic Server
 
DOC
Basic c
Veera Karthi
 
PPTX
2.Overview of C language.pptx
Vishwas459764
 
PPT
COM Introduction
Roy Antony Arnold G
 
PDF
C programming
Rounak Samdadia
 
PPTX
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
ApoorvMalviya2
 
PDF
Cpp17 and Beyond
ComicSansMS
 
PDF
data structure book in c++ and c in easy wording
yhrcxd8wpm
 
67404923-C-Programming-Tutorials-Doc.pdf
Rajb54
 
C_plus_plus
Ralph Weber
 
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
C++ Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
C tutorials
Amit Kapoor
 
Object Oriented Programming using C++ PCIT102.pdf
GauravKumar295392
 
C++ language
Hamza Asif
 
C# features
sagaroceanic11
 
Learn c language Important topics ( Easy & Logical, & smart way of learning)
Rohit Singh
 
C++ programming language basic to advance level
sajjad ali khan
 
fundamental of c++ for students of b.tech iii rd year student
Somesh Kumar
 
Gude for C++11 in Apache Traffic Server
Apache Traffic Server
 
Basic c
Veera Karthi
 
2.Overview of C language.pptx
Vishwas459764
 
COM Introduction
Roy Antony Arnold G
 
C programming
Rounak Samdadia
 
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
ApoorvMalviya2
 
Cpp17 and Beyond
ComicSansMS
 
data structure book in c++ and c in easy wording
yhrcxd8wpm
 

Recently uploaded (20)

PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Activate_Methodology_Summary presentatio
annapureddyn
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
oapresentation.pptx
mehatdhavalrajubhai
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Presentation about variables and constant.pptx
safalsingh810
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Presentation about variables and constant.pptx
kr2589474
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 

Gentle introduction to modern C++

  • 1. Gentle* Introduction to Modern C++ Mihai Todor * Well, maybe not so gentle  Disclaimer: This presentation is focused on C++11 features and assumes a basic understanding of C++
  • 2. Intro I  C++ is boring / hard to get right  The C++ compilers are buggy  We don’t want to mess with it
  • 3. Intro II And your code looks like this*: *That’s actually C# code, but you get the point 
  • 5. Intro IV In spite of the fact that it lacks some of the C# flexibility and compactness, C++ is a beautiful language, having a rich syntax, which enables a high degree of expressivity and abstraction, with a fine grained control over resource consumption.
  • 6. Intro V  If done right, in C++ you only pay for what you use  If done wrong, in C++ you blow your entire leg off, not just a toe
  • 7. Intro VI  The compiler is never* wrong  C++11 features alleviate much of the pain we had to endure when architecting code  The STL is becoming richer and more flexible  Compilers are adopting new features much faster *well, maybe sometimes, but it usually requires more than 10 minutes of coding to stumble upon a legit compiler bug
  • 8. Some modern C++ features  Move semantics  Auto  nullptr  Range-based for loops  override and final  Strongly-typed enums  Lambdas  Explicitly defaulted and deleted functions  Smart pointers
  • 9. Move semantics  Copies are expensive!  Want speed, pass by value (not always*)  Moving variables can be as cheap as a pointer swap  Classes can now have move constructors and move assignment operators * http://juanchopanzacpp.wordpress.com/2014/05/11/want-speed-dont-always-pass-by-value/
  • 10. Move semantics II  In C++11, an expression can be: The && operator has been introduced in order to declare an rvalue reference Diagram shamelessly ripped from here: http://stackoverflow.com/a/3601748/1174378
  • 11. Move semantics III  xvalue (an “eXpiring” value) – also refers to an object, usually near the end of its lifetime  A type Type followed by && represents an rvalue reference  xvalues are explicit moves, created by std::move  prvalue (“pure” rvalue) - an rvalue that is not an xvalue (implicit moves)
  • 12. Move semantics IV  A reference type that is declared using & is called an lvalue reference  A reference type that is declared using && is called an rvalue reference.
  • 13. Move semantics V  Powerful tools in the <utility> header:  std::move  std::forward  std::make_move_iterator A thorough explanation for std::move and std::forward: http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx
  • 14. auto  Repurposed keyword (initially, it designated a storage duration specifier)  Similar to C#’s var keyword  Reduces code verbosity by deducing a variable’s type from the initializer  Also allows return type deduction (C++14)  Potential for abuse!
  • 15. nullptr  Eliminates the ambiguous conversion between NULL (#defined as 0) and integral types  Denotes a value of type std::nullptr_t  Converts implicitly to bool (as false) and null pointer values of any type  You don’t have to test a variable for null with both NULL and nullptr! int *pointer = LegacyGetInt(); if (pointer != nullptr && pointer != NULL)//both tests are equivalent
  • 16. Range-based for loops  They resemble the C# foreach statement  Syntax: for (auto value: collection) {/* do stuff */}  the type specifier of value determines how the collection items are iterated: by value, by reference, by const reference, etc  Can be used with any type for which non-member overloads of the begin() and end() functions are implemented  see NonMemberBeginAndEnd.cpp
  • 17. override and final  If override is added to the declaration of a virtual method, which does not override a virtual method from the base class, the compiler emits an error  If final is added to the declaration of a virtual method, then derived classes are not allowed to override this method  in C++, the virtual specifier is optional for derived class override methods  If final is added to the declaration of a class, then no other classes can derive from that class. This resembles C#’s sealed keyword
  • 18. Strongly-typed enums  Eliminate issues caused by regular enums: namespace pollution and implicit conversions  For now, there is no simple way to use these in the same way as a C# flags enum (tagged enumeration)  The enum class underlying type can be specified by the user: enum class Colors : std::int8_t { RED = 1, GREEN = 2, BLUE = 3 };  Extracting underlying values of an enum class requires an explicit cast: auto blue = static_cast<std::underlying_type<Colors>::type>(Colors::Blue);
  • 19. Smart pointers  They provide reference counting in order to enable the automatic release of memory  Concerns about performance degradation / extra memory consumption are, indeed, legitimate, but in most cases we are not creating millions of objects on the heap to have really tight constraints  std::unique_ptr – Guarantees unique ownership of a memory resource. It cannot be copied (only moved to another std::unique_ptr).  Created with std::make_unique (C++14)  std::auto_ptr is obsolete! Please replace it on sight with std::unique_ptr.
  • 20. Smart pointers II  std::shared_ptr – Allows shared ownership of a memory resource  Created with std::make_shared (C++11)  std::weak_ptr – Holds a reference to an object managed by an std::shared_ptr.  does not contribute to the reference count  Ensures that cycles are broken  std::static_pointer_cast, std::dynamic_pointer_cast and std::const_pointer_cast are your friends!
  • 21. Lambdas  C++11 introduces support for anonymous functions, namely lambdas  Fine-grained control over variable capture  By default, variables captured by value are const, unless the mutable specification is present  We can have recursive lambdas
  • 22. Lambdas II  Syntax (shamelessly ripped from MSDN*) 1. lambda-introducer (Also known as the capture clause) 2. lambda declarator (Also known as the parameter list). Optional! 3. mutable (Also known as the mutable specification) . Optional! 4. exception-specification (Also known as the exception specification) . Optional! 5. trailing-return-type (Also known as the return type) . Optional! 6. compound-statement (Also known as the lambda body) * http://msdn.microsoft.com/en-us/library/dd293603.aspx
  • 23. Explicitly defaulted and deleted functions  The rules which specify when the implicitly- declared default constructor and destructor, copy and move constructors and copy and move assignment operators get generated are somewhat complex, so C++11 allows you to force the compiler to generate them or not, via the =default and =delete specifiers.  Please take the Rules of three/five/zero into account when using these specifiers!
  • 24. Explicitly defaulted and deleted functions II  Let’s design a trivial object, which can be constructed and moved, but not copied class UniqueObject { public: UniqueObject() =default; ~UniqueObject() =default; UniqueObject(const UniqueObject &obj) =delete; UniqueObject &operator=(const UniqueObject &obj) =delete; UniqueObject(UniqueObject &&obj) =default; UniqueObject &operator=(UniqueObject &&obj) =default; };
  • 25. More modern C++ features?  static_assert and type traits  Mutable  Constructor delegation  http://www.nullptr.me/2012/01/17/c11-delegating-constructors/  Uniform initialization  http://programmers.stackexchange.com/questions/133688/is-c11- uniform-initialization-a-replacement-for-the-old-style-syntax  rvalue reference for *this  http://stackoverflow.com/questions/12306226/what-does-an-ampersand- after-this-assignment-operator-mean  Many more…
  • 26. Follow me on: Twitter: @MihaiTodor Linkedin: www.linkedin.com/mtodor See my activity on StackOverflow: http://stackoverflow.com/users/1174378/mihai-todor