SlideShare a Scribd company logo
Sasha Goldshtein
CTO, Sela Group
What’s New in C++ 11?
Agenda
• C++ 11 status and compiler support
• New language features
• Best practices for modern C++
• What’s up with C++ 14?
We won’t cover every language feature in this
talk, e.g. variadic templates; consult Wikipedia
:-}
C++11 Compiler Support
• Support for most language features
arriving in Visual Studio 2010, 2012, 2013
Comparison chart: http://s.sashag.net/rpST0u
VisualStudio2010
Automatic
variables,
decltype
Rvalue
references
Lambda
functions
VisualStudio2012
Concurrency
library
Memory model
VisualStudio2013
Variadic
templates
Custom literals
Delegating
constructors
• Implicit variable declaration: the compiler
knows what you mean
• (Almost) necessary for anonymous types
• Very convenient for complex templates
• Easily abused by lazy programmers!
std::map<int, std::list<string>> M;
auto iter = M.begin(); //what’s the type of iter?
auto pair = std::make_pair(iter, M.key_range(...));
auto ptr = condition ? new class1 : new class2; //ERROR
auto x = 15; auto s = (string)"Hello";
auto Variables
• Automatic iterator over arrays and STL
collections
• Your collection will work – provide begin(), end(),
and an input iterator over the elements
• Can also specialize global std::begin(), std::end() if
you don’t control the collection class
int numbers[] = ...;
for (int n : numbers) std::cout << n;
std::map<std::string,std::list<int>> M;
for (const auto& pair : M)
for (auto n : pair.second)
std::cout << pair.first << ' ' << pair.second;
Range-Based for Loop
• Initialize arrays, lists, vectors, other containers—
and your own containers—with a natural syntax
• Also applies to structs/classes!
vector<int> v { 1, 2, 3, 4 };
list<string> l = { ‚Tel-Aviv‛, ‚London‛ };
my_cont c { 42, 43, 44 };
point origin { 0, 0 }; //not a container, but has ctor taking two ints
class my_cont {
public: my_cont(std::initializer_list<int> list) {
for (auto it = list.begin(); it != list.end(); ++it) . . .
}
};
Initializer Lists
• Some people now use auto exclusively
• Beauty is in the eyes of the beholder…
auto flags = DWORD { FILE_FLAG_NO_BUFFERING };
auto numbers = std::vector<int> { 1, 2, 3 };
auto event_handle = HANDLE {};
// You can consistenly use ‚auto‛ with functions, too:
auto is_prime(unsigned n) -> bool;
auto read_async(HANDLE h, unsigned count) -> vector<char>;
Taking It Further…
int main() {
[](){}();
[]{}();
} //this is legal C++,
//although not useful
Lambda Functions
• Inline methods in other methods
std::list<int> ns = ...;
auto print_num = [](int n) { std::cout << n; };
std::for_each(ns.begin(), ns.end(), print_num);
int even = std::count_if(ns.begin(), ns.end(),
[](int n) { return n&1==0; });
// Print squares
std::transform(ns.begin(), ns.end(),
std::ostream_iterator<int>(std::cout, 'n'),
[](int n) { return n*n; });
Lambda Functions
• Capturing external local variables
• Compiled to a function object with fields
int fib1 = 1, fib2 = 1;
auto next_step = [&]() { //capture by reference
int temp = fib2; fib2 = fib2 + fib1; fib1 = temp;
};
for (int i = 0; i < 20; ++i) next_step();
std::thread t([=]() { // capture by value
std::cout << fib1 << std::endl;
});
t.join();
More Fun With Lambdas
• Design your APIs with lambdas in mind
• Usually through template parameters
• Invest in re-learning STL algorithms
template <typename Callback>
void enum_windows(const string& title, Callback callback) {
. . . callback(current_window);
}
template <typename RAIter, typename Comparer>
void superfast_sort(RAIter from, RAIter to, Comparer cmp);
Applying Lambdas
• Stateless lambdas are convertible to
function pointers!
• Useful for interop with C-style APIs that take
function pointers (e.g., Win32)
ReadFileEx(file, buffer, 4096, &overlapped,
[](DWORD ec, DWORD count, LPOVERLAPPED overlapped) {
// process the results
}
);
Lambdas and Function Pointers
Modern C++ Style
• Use auto, for, initializer lists ubiquitously
• OK to design algorithms that require
predicates, projections, and other functors
– They will be easy to use—with lambda
functions
• Use STL algorithms more widely with
lambdas
• Lvalues are values that have a name
• Can appear on the l.h.s. of an assignment
• Rvalues are the rest
int x;
x = 42; //OK, x has a name, it’s an lvalue
x + 2 = 42; //Wrong, x + 2 returns an rvalue
x++ = 42; //Also wrong, x++ is an rvalue
int& foo();
int* goo();
--foo(); //OK, foo() returns an lvalue
++(*goo()); //OK, a dereferenced pointer is an lvalue
Rvalues and Lvalues—Reminder
• Observation: rvalues are unnamed, and
are destroyed promptly
template <typename Param> void func(Param p);
// The vector is destroyed after func is called
func(std::vector<int>());
// The result of operator+ is destroyed after func is called
func(matrixA + matrixB);
Who Cares?
• The standard approach to references
limits the performance of the language
// In ‚old‛ C++, this requires a string copy per iteration
// What’s worse, when the vector expands  more copies
std::vector<std::string> strings;
for (int i = 0; i < 100000; ++i)
{
strings.push_back(‚Hello‛);
}
Who Cares?
my_array(const my_array& other) { //copy ctor
dataptr_ = new T[size_ = other.size_];
memcpy_s(dataptr_, size_*sizeof(T), other.dataptr_,
size_*sizeof(T));
}
my_array& operator=(const my_array& other) { /*same deal*/ }
my_array& operator=(my_array&& other) { //move assignment
dataptr_ = other.dataptr_; size_ = other.size_;
other.dataptr_ = nullptr; other.size_ = 0;
}
my_array(my_array&& other) { //move ctor
*this = std::move(other); //NOTE: other is an lvalue :-{
}
&& Enable Move Construction
Guidelines for Move Semantics
• Don’t be afraid to return moveable objects by
value
– Best case: RVO/NRVO kicks in to optimize
completely
– Worst case: the object is moved, almost no
overhead
• Pass objects by value to sinks
– In other words, move them to sinks
• Non-copyable objects might be still suitable
for moving
– E.g., unique_ptr<T>, std::thread, lambdas, …
Deleted and Defaulted Functions
• Disallow a certain function or use its
default implementation
• Usually used with ctors and operator=
class thread {
public:
template <typename Fn> thread(Fn&& fn);
thread(const thread&) = delete;
thread(thread&&) = delete;
~thread() = default;
// ...
};
Delegating Constructors
• No more ―init‖ functions! You can now
call a constructor from another
constructor
class file {
public:
file(string filename) : file(filename.c_str()) {}
file(const char* filename) : file(open(filename)) {}
file(int fd) {
// Actual initialization goes here
}
};
Alias Templates
• ―Typedefs‖ can now be templates
• The using keyword can replace typedef
completely
template <typename K, typename V, typename Alloc>
class map;
template <typename T>
using simple_set<T> = map<T, bool, DefaultAllocator>;
using thread_func = DWORD(*)(LPVOID);
Non-Static Data Member
Initializers
• Data members can be initialized inline
• The compiler adds the appropriate code
prior to each constructor
class counter {
int count = 0;
SRWLOCK lock { SRWLOCK_INIT };
public:
// ...
};
More Miscellaneous Features
• Explicit conversion operators
• Raw string literals
• Custom literals
auto utf8string = u8"Hello";
auto utf16string = u"Hello";
auto utf32string = U"Hello";
auto raw = R"(I can put " here and also )";
auto break_time = 5min; // in the STL as of C++14
New Smart Pointers
• STL now has three types of smart pointers,
eliminating the need to ever use delete
– If you are the sole owner of the object, use
unique_ptr to make sure it’s deleted when
the pointer dies (RAII)
– If you want to share the object with others,
use shared_ptr—it will perform smart
reference counting
– If you got yourself a cycle, use weak_ptr to
break it!
• Sole owner of an object
• Moveable, not copyable
• Replaces auto_ptr (which can’t move!)
unique_ptr<expensive_thing> create() {
unique_ptr<expensive_thing> p(new expensive_thing);
// ...do some initialization, exceptions are covered by RAII
return p;
}
unique_ptr<expensive_thing> p = create(); // move constructor used!
//another example is storing pointers in containers:
vector<unique_ptr<string>> v = { new string(‚A‛), new string(‚B‛) };
unique_ptr
• Thread-safe reference-counted pointer to an
object with shared ownership
• When the last pointer dies, the object is deleted
struct file_handle {
HANDLE handle;
file_handle(const string& filename) ...
~file_handle() ... //closes the handle
};
class file {
shared_ptr<file_handle> _handle;
public:
file(const string& filename) : _handle(new file_handle(filename)) {}
file(shared_ptr<file_handle> fh) : _handle(fh) {}
}; //can have multiple file objects over the same file_handle
shared_ptr
• Points to a shared object but does not keep
it alive (does not affect reference count)
• Breaks cycles between shared_ptrs
class employee {
weak_ptr<employee> _manager;
vector<shared_ptr<employee>> _direct_reports;
public:
void beg_for_vacation(int days) {
if (auto mgr = _manager.lock()) { mgr->beg(days); }
else { /* your manager has been eliminated :-) */ }
}
};
weak_ptr
Guidance for Smart Pointers
• Own memory with smart pointers: don’t delete
• Allocate shared pointers with make_shared
• Most owners of pointers can use unique_ptr,
which is more efficient than shared_ptr
• Don’t use smart pointers for passing parameters if
the usage lifetime is a subset of the function call’s
lifetime
• Pass unique_ptr<T> by value to a sink that retains
the object pointer and will destroy it
• Pass shared_ptr<T> by value to express taking
shared ownership of the object
struct window {
// Okay – takes ownership of the window, will destroy it.
void set_owned_child(unique_ptr<window> window) {
child = std::move(window);
}
unique_ptr<window> child;
};
// Weird – use plain & or * instead.
currency calculate_tax(const unique_ptr<employee>& emp);
// Okay – takes shared ownership of the callback,
// lifetime has to be extended past the call site
void set_callback(shared_ptr<callback> cb);
Guidance for Smart Pointers:
Examples
In C++ 14, you write
auto auto(auto auto) { auto; }
and the compiler infers the rest from context.
In C++ 14…
• Lambda parameters can be declared with the
auto specifier
• This is not type deduction – this is a template!
auto lambda = [](auto f) { f.foo(); };
// equivalent to:
struct __unnamed__ {
template <typename S>
void operator()(S f) const { f.foo(); }
};
auto lambda = __unnamed__();
Generic Lambdas
• Lambdas can now capture arbitrary
expressions, which enables renaming and
capture-by-move semantics
std::async([tid = std::this_thread::get_id()]() {
cout << ‚called from thread ‚ << tid << endl;
});
unique_ptr<window> top_level = ...;
auto f = [top = std::move(top_level)]() {
// access ‘top’
};
Lambda Capture Extensions
• Functions can be declared as returning
auto, and the compiler deduces the
return type (if consistent)
// Not okay, return type unknown at recursive call:
auto fibonacci(int n) {
if (n != 1 && n != 2) return fibonacci(n-1) + fibonacci(n-2);
return 1;
}
auto fibonacci(int n) { // Okay, return type deduced to be int
if (n == 1 || n == 2) return 1;
return fibonacci(n-1) + fibonacci(n-2);
}
Function Return Type Deduction
Miscellaneous Language
Features
• Variable templates
template <typename T> T MATH_PI;
• Runtime-sized arrays
• Binary literals 0b10011101
Library Features
• Shared mutex (reader-writer lock)
• User-defined literals for std::strings
(s‛hello‛) and duration (3h, 50min,
10ms, and others)
• Optional type optional<T>
Summary
• Modern C++ is nothing like the 1990’s
• Things are looking up for C++ developers
• Lots of language and library changes
• Faster library evolution going forward
THANK YOU!
Sasha Goldshtein
CTO, Sela Group
blog.sashag.net
@goldshtn

More Related Content

PDF
Modern C++ Explained: Move Semantics (Feb 2018)
PPTX
The Style of C++ 11
PPTX
PPTX
C++ 11 Features
PDF
C++11 Idioms @ Silicon Valley Code Camp 2012
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
PPTX
PPTX
Modern C++ Explained: Move Semantics (Feb 2018)
The Style of C++ 11
C++ 11 Features
C++11 Idioms @ Silicon Valley Code Camp 2012
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...

What's hot (20)

PDF
C++11: Rvalue References, Move Semantics, Perfect Forwarding
PDF
C++11 & C++14
PDF
Modern C++
PPTX
C++11: Feel the New Language
PPTX
PPTX
What's New in C++ 11/14?
PDF
Modern c++ (C++ 11/14)
PDF
C++11 concurrency
PDF
Smart Pointers in C++
PPT
STL ALGORITHMS
PPT
Gentle introduction to modern C++
PPTX
Fun with Lambdas: C++14 Style (part 2)
PPTX
Smart pointers
PPTX
Summary of C++17 features
PDF
C++11 smart pointer
PPTX
C++ Presentation
PDF
C++20 the small things - Timur Doumler
PDF
Fun with Lambdas: C++14 Style (part 1)
PDF
C++17 introduction - Meetup @EtixLabs
PDF
C++ references
C++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11 & C++14
Modern C++
C++11: Feel the New Language
What's New in C++ 11/14?
Modern c++ (C++ 11/14)
C++11 concurrency
Smart Pointers in C++
STL ALGORITHMS
Gentle introduction to modern C++
Fun with Lambdas: C++14 Style (part 2)
Smart pointers
Summary of C++17 features
C++11 smart pointer
C++ Presentation
C++20 the small things - Timur Doumler
Fun with Lambdas: C++14 Style (part 1)
C++17 introduction - Meetup @EtixLabs
C++ references
Ad

Similar to What's New in C++ 11? (20)

PPTX
Return of c++
PPTX
Whats New in Visual Studio 2012 for C++ Developers
PPTX
Modern C++
PDF
An Overview Of Standard C++Tr1
PDF
Bjarne essencegn13
PPTX
Modern C++ Lunch and Learn
PDF
C++ Training
PDF
(4) cpp automatic arrays_pointers_c-strings
PDF
Meetup C++ A brief overview of c++17
PDF
STL in C++
PPTX
C++11 - STL Additions
PDF
Milot Shala - C++ (OSCAL2014)
PPTX
Useful C++ Features You Should be Using
PPT
Glimpses of C++0x
PDF
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
PDF
C++ tutorial boost – 2013
PDF
C++11 talk
PPTX
Elements of C++11
PPTX
C traps and pitfalls for C++ programmers
PPTX
C++ Introduction brown bag
Return of c++
Whats New in Visual Studio 2012 for C++ Developers
Modern C++
An Overview Of Standard C++Tr1
Bjarne essencegn13
Modern C++ Lunch and Learn
C++ Training
(4) cpp automatic arrays_pointers_c-strings
Meetup C++ A brief overview of c++17
STL in C++
C++11 - STL Additions
Milot Shala - C++ (OSCAL2014)
Useful C++ Features You Should be Using
Glimpses of C++0x
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
C++ tutorial boost – 2013
C++11 talk
Elements of C++11
C traps and pitfalls for C++ programmers
C++ Introduction brown bag
Ad

More from Sasha Goldshtein (20)

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

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
NewMind AI Monthly Chronicles - July 2025
PPT
Teaching material agriculture food technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
NewMind AI Monthly Chronicles - July 2025
Teaching material agriculture food technology
The AUB Centre for AI in Media Proposal.docx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf

What's New in C++ 11?

  • 1. Sasha Goldshtein CTO, Sela Group What’s New in C++ 11?
  • 2. Agenda • C++ 11 status and compiler support • New language features • Best practices for modern C++ • What’s up with C++ 14? We won’t cover every language feature in this talk, e.g. variadic templates; consult Wikipedia :-}
  • 3. C++11 Compiler Support • Support for most language features arriving in Visual Studio 2010, 2012, 2013 Comparison chart: http://s.sashag.net/rpST0u VisualStudio2010 Automatic variables, decltype Rvalue references Lambda functions VisualStudio2012 Concurrency library Memory model VisualStudio2013 Variadic templates Custom literals Delegating constructors
  • 4. • Implicit variable declaration: the compiler knows what you mean • (Almost) necessary for anonymous types • Very convenient for complex templates • Easily abused by lazy programmers! std::map<int, std::list<string>> M; auto iter = M.begin(); //what’s the type of iter? auto pair = std::make_pair(iter, M.key_range(...)); auto ptr = condition ? new class1 : new class2; //ERROR auto x = 15; auto s = (string)"Hello"; auto Variables
  • 5. • Automatic iterator over arrays and STL collections • Your collection will work – provide begin(), end(), and an input iterator over the elements • Can also specialize global std::begin(), std::end() if you don’t control the collection class int numbers[] = ...; for (int n : numbers) std::cout << n; std::map<std::string,std::list<int>> M; for (const auto& pair : M) for (auto n : pair.second) std::cout << pair.first << ' ' << pair.second; Range-Based for Loop
  • 6. • Initialize arrays, lists, vectors, other containers— and your own containers—with a natural syntax • Also applies to structs/classes! vector<int> v { 1, 2, 3, 4 }; list<string> l = { ‚Tel-Aviv‛, ‚London‛ }; my_cont c { 42, 43, 44 }; point origin { 0, 0 }; //not a container, but has ctor taking two ints class my_cont { public: my_cont(std::initializer_list<int> list) { for (auto it = list.begin(); it != list.end(); ++it) . . . } }; Initializer Lists
  • 7. • Some people now use auto exclusively • Beauty is in the eyes of the beholder… auto flags = DWORD { FILE_FLAG_NO_BUFFERING }; auto numbers = std::vector<int> { 1, 2, 3 }; auto event_handle = HANDLE {}; // You can consistenly use ‚auto‛ with functions, too: auto is_prime(unsigned n) -> bool; auto read_async(HANDLE h, unsigned count) -> vector<char>; Taking It Further…
  • 8. int main() { [](){}(); []{}(); } //this is legal C++, //although not useful Lambda Functions
  • 9. • Inline methods in other methods std::list<int> ns = ...; auto print_num = [](int n) { std::cout << n; }; std::for_each(ns.begin(), ns.end(), print_num); int even = std::count_if(ns.begin(), ns.end(), [](int n) { return n&1==0; }); // Print squares std::transform(ns.begin(), ns.end(), std::ostream_iterator<int>(std::cout, 'n'), [](int n) { return n*n; }); Lambda Functions
  • 10. • Capturing external local variables • Compiled to a function object with fields int fib1 = 1, fib2 = 1; auto next_step = [&]() { //capture by reference int temp = fib2; fib2 = fib2 + fib1; fib1 = temp; }; for (int i = 0; i < 20; ++i) next_step(); std::thread t([=]() { // capture by value std::cout << fib1 << std::endl; }); t.join(); More Fun With Lambdas
  • 11. • Design your APIs with lambdas in mind • Usually through template parameters • Invest in re-learning STL algorithms template <typename Callback> void enum_windows(const string& title, Callback callback) { . . . callback(current_window); } template <typename RAIter, typename Comparer> void superfast_sort(RAIter from, RAIter to, Comparer cmp); Applying Lambdas
  • 12. • Stateless lambdas are convertible to function pointers! • Useful for interop with C-style APIs that take function pointers (e.g., Win32) ReadFileEx(file, buffer, 4096, &overlapped, [](DWORD ec, DWORD count, LPOVERLAPPED overlapped) { // process the results } ); Lambdas and Function Pointers
  • 13. Modern C++ Style • Use auto, for, initializer lists ubiquitously • OK to design algorithms that require predicates, projections, and other functors – They will be easy to use—with lambda functions • Use STL algorithms more widely with lambdas
  • 14. • Lvalues are values that have a name • Can appear on the l.h.s. of an assignment • Rvalues are the rest int x; x = 42; //OK, x has a name, it’s an lvalue x + 2 = 42; //Wrong, x + 2 returns an rvalue x++ = 42; //Also wrong, x++ is an rvalue int& foo(); int* goo(); --foo(); //OK, foo() returns an lvalue ++(*goo()); //OK, a dereferenced pointer is an lvalue Rvalues and Lvalues—Reminder
  • 15. • Observation: rvalues are unnamed, and are destroyed promptly template <typename Param> void func(Param p); // The vector is destroyed after func is called func(std::vector<int>()); // The result of operator+ is destroyed after func is called func(matrixA + matrixB); Who Cares?
  • 16. • The standard approach to references limits the performance of the language // In ‚old‛ C++, this requires a string copy per iteration // What’s worse, when the vector expands  more copies std::vector<std::string> strings; for (int i = 0; i < 100000; ++i) { strings.push_back(‚Hello‛); } Who Cares?
  • 17. my_array(const my_array& other) { //copy ctor dataptr_ = new T[size_ = other.size_]; memcpy_s(dataptr_, size_*sizeof(T), other.dataptr_, size_*sizeof(T)); } my_array& operator=(const my_array& other) { /*same deal*/ } my_array& operator=(my_array&& other) { //move assignment dataptr_ = other.dataptr_; size_ = other.size_; other.dataptr_ = nullptr; other.size_ = 0; } my_array(my_array&& other) { //move ctor *this = std::move(other); //NOTE: other is an lvalue :-{ } && Enable Move Construction
  • 18. Guidelines for Move Semantics • Don’t be afraid to return moveable objects by value – Best case: RVO/NRVO kicks in to optimize completely – Worst case: the object is moved, almost no overhead • Pass objects by value to sinks – In other words, move them to sinks • Non-copyable objects might be still suitable for moving – E.g., unique_ptr<T>, std::thread, lambdas, …
  • 19. Deleted and Defaulted Functions • Disallow a certain function or use its default implementation • Usually used with ctors and operator= class thread { public: template <typename Fn> thread(Fn&& fn); thread(const thread&) = delete; thread(thread&&) = delete; ~thread() = default; // ... };
  • 20. Delegating Constructors • No more ―init‖ functions! You can now call a constructor from another constructor class file { public: file(string filename) : file(filename.c_str()) {} file(const char* filename) : file(open(filename)) {} file(int fd) { // Actual initialization goes here } };
  • 21. Alias Templates • ―Typedefs‖ can now be templates • The using keyword can replace typedef completely template <typename K, typename V, typename Alloc> class map; template <typename T> using simple_set<T> = map<T, bool, DefaultAllocator>; using thread_func = DWORD(*)(LPVOID);
  • 22. Non-Static Data Member Initializers • Data members can be initialized inline • The compiler adds the appropriate code prior to each constructor class counter { int count = 0; SRWLOCK lock { SRWLOCK_INIT }; public: // ... };
  • 23. More Miscellaneous Features • Explicit conversion operators • Raw string literals • Custom literals auto utf8string = u8"Hello"; auto utf16string = u"Hello"; auto utf32string = U"Hello"; auto raw = R"(I can put " here and also )"; auto break_time = 5min; // in the STL as of C++14
  • 24. New Smart Pointers • STL now has three types of smart pointers, eliminating the need to ever use delete – If you are the sole owner of the object, use unique_ptr to make sure it’s deleted when the pointer dies (RAII) – If you want to share the object with others, use shared_ptr—it will perform smart reference counting – If you got yourself a cycle, use weak_ptr to break it!
  • 25. • Sole owner of an object • Moveable, not copyable • Replaces auto_ptr (which can’t move!) unique_ptr<expensive_thing> create() { unique_ptr<expensive_thing> p(new expensive_thing); // ...do some initialization, exceptions are covered by RAII return p; } unique_ptr<expensive_thing> p = create(); // move constructor used! //another example is storing pointers in containers: vector<unique_ptr<string>> v = { new string(‚A‛), new string(‚B‛) }; unique_ptr
  • 26. • Thread-safe reference-counted pointer to an object with shared ownership • When the last pointer dies, the object is deleted struct file_handle { HANDLE handle; file_handle(const string& filename) ... ~file_handle() ... //closes the handle }; class file { shared_ptr<file_handle> _handle; public: file(const string& filename) : _handle(new file_handle(filename)) {} file(shared_ptr<file_handle> fh) : _handle(fh) {} }; //can have multiple file objects over the same file_handle shared_ptr
  • 27. • Points to a shared object but does not keep it alive (does not affect reference count) • Breaks cycles between shared_ptrs class employee { weak_ptr<employee> _manager; vector<shared_ptr<employee>> _direct_reports; public: void beg_for_vacation(int days) { if (auto mgr = _manager.lock()) { mgr->beg(days); } else { /* your manager has been eliminated :-) */ } } }; weak_ptr
  • 28. Guidance for Smart Pointers • Own memory with smart pointers: don’t delete • Allocate shared pointers with make_shared • Most owners of pointers can use unique_ptr, which is more efficient than shared_ptr • Don’t use smart pointers for passing parameters if the usage lifetime is a subset of the function call’s lifetime • Pass unique_ptr<T> by value to a sink that retains the object pointer and will destroy it • Pass shared_ptr<T> by value to express taking shared ownership of the object
  • 29. struct window { // Okay – takes ownership of the window, will destroy it. void set_owned_child(unique_ptr<window> window) { child = std::move(window); } unique_ptr<window> child; }; // Weird – use plain & or * instead. currency calculate_tax(const unique_ptr<employee>& emp); // Okay – takes shared ownership of the callback, // lifetime has to be extended past the call site void set_callback(shared_ptr<callback> cb); Guidance for Smart Pointers: Examples
  • 30. In C++ 14, you write auto auto(auto auto) { auto; } and the compiler infers the rest from context. In C++ 14…
  • 31. • Lambda parameters can be declared with the auto specifier • This is not type deduction – this is a template! auto lambda = [](auto f) { f.foo(); }; // equivalent to: struct __unnamed__ { template <typename S> void operator()(S f) const { f.foo(); } }; auto lambda = __unnamed__(); Generic Lambdas
  • 32. • Lambdas can now capture arbitrary expressions, which enables renaming and capture-by-move semantics std::async([tid = std::this_thread::get_id()]() { cout << ‚called from thread ‚ << tid << endl; }); unique_ptr<window> top_level = ...; auto f = [top = std::move(top_level)]() { // access ‘top’ }; Lambda Capture Extensions
  • 33. • Functions can be declared as returning auto, and the compiler deduces the return type (if consistent) // Not okay, return type unknown at recursive call: auto fibonacci(int n) { if (n != 1 && n != 2) return fibonacci(n-1) + fibonacci(n-2); return 1; } auto fibonacci(int n) { // Okay, return type deduced to be int if (n == 1 || n == 2) return 1; return fibonacci(n-1) + fibonacci(n-2); } Function Return Type Deduction
  • 34. Miscellaneous Language Features • Variable templates template <typename T> T MATH_PI; • Runtime-sized arrays • Binary literals 0b10011101
  • 35. Library Features • Shared mutex (reader-writer lock) • User-defined literals for std::strings (s‛hello‛) and duration (3h, 50min, 10ms, and others) • Optional type optional<T>
  • 36. Summary • Modern C++ is nothing like the 1990’s • Things are looking up for C++ developers • Lots of language and library changes • Faster library evolution going forward
  • 37. THANK YOU! Sasha Goldshtein CTO, Sela Group blog.sashag.net @goldshtn