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

What's hot

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 ForwardingFrancesco Casalegno
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Languagemspline
 
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?Dina Goldshtein
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrencyxu liwei
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMSfawzmasood
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++Mihai Todor
 
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)Sumant Tambe
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointerLei Yu
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumlercorehard_by
 
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)Sumant Tambe
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsStephane Gleizes
 

What's hot (20)

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
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
Modern C++
Modern C++Modern C++
Modern C++
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
C++11
C++11C++11
C++11
 
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?
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
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)
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointer
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
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
 
C++ references
C++ referencesC++ references
C++ references
 

Similar to What's New in C++ 11 and Beyond: Language Features and Best Practices

Similar to What's New in C++ 11 and Beyond: Language Features and Best Practices (20)

Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
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
 
C
CC
C
 
Return of c++
Return of c++Return of c++
Return of c++
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
C++ language
C++ languageC++ language
C++ language
 
Experimental dtrace
Experimental dtraceExperimental dtrace
Experimental dtrace
 
C++primer
C++primerC++primer
C++primer
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
 
02.adt
02.adt02.adt
02.adt
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
C language
C languageC language
C language
 
C programming
C programmingC programming
C programming
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 

More from Sasha Goldshtein

Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing LandscapeSasha Goldshtein
 
The Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF PrimerThe Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF PrimerSasha Goldshtein
 
Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF AbyssSasha Goldshtein
 
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 FrameworkSasha Goldshtein
 
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 XSasha Goldshtein
 
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 XamarinSasha Goldshtein
 
Modern Backends for Mobile Apps
Modern Backends for Mobile AppsModern Backends for Mobile Apps
Modern Backends for Mobile AppsSasha Goldshtein
 
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 2013Sasha Goldshtein
 
Mastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and ProductionMastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and ProductionSasha Goldshtein
 
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 MinutesSasha Goldshtein
 
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 BackendSasha Goldshtein
 
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 ServicesSasha Goldshtein
 
Attacking Web Applications
Attacking Web ApplicationsAttacking Web Applications
Attacking Web ApplicationsSasha Goldshtein
 
Windows Azure Mobile Services
Windows Azure Mobile ServicesWindows Azure Mobile Services
Windows Azure Mobile ServicesSasha Goldshtein
 
First Steps in Android Development
First Steps in Android DevelopmentFirst Steps in Android Development
First Steps in Android DevelopmentSasha Goldshtein
 
First Steps in iOS Development
First Steps in iOS DevelopmentFirst Steps in iOS Development
First Steps in iOS DevelopmentSasha 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

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Recently uploaded (20)

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

What's New in C++ 11 and Beyond: Language Features and Best Practices

  • 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