SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
Modern C++ 
(less == more) || (more == more) 
! 
mailto:michael@meta.sg
History 
1958!!ALGOL!58!!(Bauer,(Backus(et(al) 
1957!!FORTRAN!!(John(Backus) 
1969!!C!!(Dennis(Ritchie) 
1968!!ALGOL!68 
1966!!FORTRAN!66 
1983!!AT&T!C++!/!Cfront!!(Bjarne(Stroustrup) 
1981!!Objec:ve<C!!(Brad(Cox(and(Tom(Love) 
1979!!C!with!Classes!!(Bjarne(Stroustrup) 
1978!!K&R!C!!(Brian(Kernighan(and(Dennis(Ritchie) 
1977!!FORTRAN!77 
1989!!ANSI/ISO!C89 
1987!!GNU!C!Released 
2003!!ISO!C++03 
1999!ISO!C99 
1998!!ISO!C++98 
2017!!ISO!C++17 
2014!!ISO!C++14 
2011!!ISO!C++11 
2008!!Clang!Released 
2008!!ISO!C++!TR1 
1950 1960 1970 1980 1990 2000 2010 2020
What is C++? 
• A general purpose programming language 
• Has a strong bias towards systems programming 
• One layer above assembly language 
int do_rdrand() 
{ 
• Multi-paradigm language 
int r; 
asm("retry: rdrand eaxn” 
" jnc retry;n" 
: "=a" (r)); 
return r; 
• Imperative (C99 superset) 
} 
• Object-oriented (polymorphism, inheritance) 
• Functional (immutability, lambdas, currying) 
• Meta-programming (templates, algorithms)
Why C++ 
• ISO/IEC 14882:2011 
• International standards organisation represented 
by national bodies from 164 member countries 
• Not open to manipulation by individuals or 
corporations for proprietary control 
• COBOL, Fortran, Ada, Prolog, C, C++, Forth, 
ECMAscript 
• C++ is a software engineering language that 
enables the production of high performance 
type-safe, statically verifiable native software.
Why C++ 
• C++ is popular 
Source: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
C++ projects 
• GNU Compiler Collection, LLVM, Clang 
• Java Virtual Machine, Dart, V8, Facebook HHVM 
• Webkit, Chrome, Safari, Firefox, Internet Explorer 
• MySQL, LevelDB, MongoDB, SAP DB 
• Adobe, Autodesk, Apple, Microsoft, … 
• Scientific computing, Computer Aided Engineering, 
Operations Research, Telecommunications, … 
• Bitcoin ☺
What is Modern C++? 
• Modern C++! 
• feels like “a new language” (Bjarne Stroustrup) 
• C++11 refines the C++ language in a backwards 
compatible way to support existing C++ code while 
enabling simpler and more modern idioms 
• Pass-by-value everywhere 
• STL containers got smarter with r-value references 
• Lambdas, initializer lists, variadic templates, etc 
• Improved memory management 
• Improved standard library
Less is exponentially more 
“I was asked a few weeks ago, "What was the biggest surprise 
you encountered rolling out Go?" I knew the answer instantly: 
Although we expected C++ programmers to see Go as an 
alternative, instead most Go programmers come from languages 
like Python and Ruby. Very few come from C++. 
We—Ken, Robert and myself—were C++ programmers when we 
designed a new language to solve the problems that we thought 
needed to be solved for the kind of software we wrote. It seems 
almost paradoxical that other C++ programmers don't seem to 
care.” –Rob Pike 
http://commandcenter.blogspot.com/2012/06/less-is-exponentially-more.html
C++11 has got more 
std::future 
auto lambda 
<functional> 
std::shared_ptr <thread> 
std::async 
<random> 
<type_traits> 
<memory> 
constexpr 
decltype 
<regex> <mutex> 
std::tuple 
std::unordered_map 
std::unordered_set 
nullptr 
<atomic> static_assert 
std::unique_ptr 
<chrono>
C++11 has got less 
C++98 C++11 
#include <iostream> 
#include <vector> 
! 
using namespace std; 
! 
int main() 
{ 
vector<int> v; 
v.push_back(2); 
v.push_back(3); 
v.push_back(5); 
v.push_back(7); 
vector<int>::iterator i; 
for (i = v.begin(); i != v.end(); i++) { 
cout << *i << endl; 
} 
} 
#include <iostream> 
#include <vector> 
! 
using namespace std; 
! 
int main() 
{ 
vector<int> v = {2,3,5,7}; 
for (auto a : v) { 
cout << a << endl; 
} 
} 
or even less 
int main() 
{ 
for (auto a : {2,3,5,7}) 
cout << a << endl; 
}
Template Metaprogramming 
• The C++ template system is turing complete at compile time 
#include <iostream> 
// http://stackoverflow.com/questions/3082113/calculating-factorial-using-template-meta-programming 
! 
using namespace std; 
! 
template <int N> 
struct Factorial 
{ 
enum { value = N * Factorial<N - 1>::value }; 
}; 
! 
template <> 
struct Factorial<0> 
{ 
enum { value = 1 }; 
}; 
! 
int main() 
{ 
cout << Factorial<4>::value << endl; 
cout << Factorial<0>::value << endl; 
}
Zero-copy layout 
OO Language C++11 
class Price 
{ 
int timestamp; 
int volume; 
double price; 
Price(int timestamp, int volume, double price) 
{ 
this.timestamp = timestamp; 
this.volume = volume; 
this.price = price; 
} 
static void main(String args[]) 
{ 
ArrayList<Price> prices; 
prices.ensureCapacity(4); 
prices.add(new Price(1307725482, 5, 26.05)); 
prices.add(new Price(1307725483, 40, 26.15)); 
prices.add(new Price(1307725493, 100, 26.1499)); 
prices.add(new Price(1307725493, 112, 26.15)); 
} 
} 
struct Price 
{ 
int timestamp; 
int volume; 
double price; 
Price(int timestamp, int volume, double price) 
: timestamp(timestamp), volume(volume), price(price) {} 
}; ! 
int main() 
{ 
vector<Price> prices; 
prices.reserve(4); 
prices.emplace_back(1307725482, 5, 26.05); 
prices.emplace_back(1307725483, 40, 26.15); 
prices.emplace_back(1307725493, 100, 26.1499); 
prices.emplace_back(1307725493, 112, 26.15); 
} 
• Containers use polymorphism and type erase 
• Need to devolve to structure of primitive arrays 
to get reasonable performance, but SoA has 
bad cache behaviour 
• Looks are deceiving 
• Templates instantiate optimized code for the type 
• Superset - allows implementation of polymorphic 
containers i.e. vector<object>
Zero-copy layout 
OO Language - 176 bytes, 5 allocations 
Opaque Object Header (16-bytes) array object reference 
size 
length 
Opaque Object Header (16-bytes) 
Opaque Object Header (16-bytes) 
64-bit Object Reference (8-bytes) 64-bit Object Reference (8-bytes) 64-bit Object Reference (8-bytes) 
Opaque Object Header (16-bytes) 
Opaque Object Header (16-bytes) 
1307725482 5 26.05 
1307725483 40 26.15 
1307725493 100 26.1499 
C++11 - 72 bytes, 2 allocations 
array ptr size capacity 
1307725482 5 26.05 1307725483 40 26.15 1307725493 100 26.1499
No more leaks 
• std::unique_ptr 
• Singleton pointer wrapper 
• Ensures a single copy of an object 
• No performance overhead 
• Automatic release (no more delete) 
• std::shared_ptr 
• Reference counting pointer wrapper 
• Thread-safe reference counter 
• Acts like a normal pointer but has overhead 
• Use when correctness is more important than performance 
• Automatic release (no more delete)
R-value references 
• template<typename T> void foo(T&&) 
• New signature to detect r-values (temporaries) 
• In a nut shell enables compile time detection of 
r-value temporaries (versus l-values) to implement efficient 
zero-copy move semantics and perfect forwarding 
• What is an r-value? 
vector<Price> raise(vector<Price> prices) { /* does something */ } 
raise({Price(1307725482, 5, 26.05), Price(1307725483, 40, 26.15)}); 
• Adds complexity to hide complexity 
• Feature designed for STL and library writers 
• std::forward, std::move 
• Efficient pass by value and return by value for containers 
• Simplifies idiom of user code
Initializer lists 
• Array initialization 
was a C++98 bugbear 
• Adds new initializer syntax 
foo{1,2,3,4} 
• Can be used in constructor 
member initialization 
• New header 
<initializer_list> 
• New template 
initializer_list<T> 
struct myclass { 
myclass (int,int); 
myclass (initializer_list<int>); 
/* definitions ... */ 
}; ! 
myclass foo {10,20}; // calls initializer_list ctor 
myclass bar (10,20); // calls first constructor 
template <typename T> 
struct vec 
{ 
vec(T x) : m{x, 0, 0, 1} {} 
vec(T x, T y) : m{x, y, 0, 1} {} 
vec(T x, T y, T z) : m{x, y, z, 1} {} 
vec(T x, T y, T z, T w) : m{x, y, z, w} {} 
T m[4]; 
}; 
! 
vec<float> a(1, 2, 3);
Delegating constructors 
• Really. C++98 didn’t have them 
template <typename T> 
struct vec 
{ 
vec(T x) : vec(x, 0, 0, 1) {} 
vec(T x, T y) : vec(x, y, 0, 1) {} 
vec(T x, T y, T z) : vec(x, y, z, 1) {} 
vec(T x, T y, T z, T w) : m{x, y, z, w} {} 
T m[4]; 
};
Threads and Futures 
• std::thread 
• std::async 
• std::future 
• C++ is in now line 
with other modern 
languages thread 
capabilities 
#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <numeric> 
#include <future> ! 
// source http://en.cppreference.com/w/cpp/thread/async ! 
template <typename I> 
int parallel_sum(I beg, I end) 
{ 
typename I::difference_type len = end - beg; 
if (len < 1000) { 
return std::accumulate(beg, end, 0); 
} 
I mid = beg + len/2; 
auto handle = std::async(std::launch::async, 
parallel_sum<I>, mid, end); 
int sum = parallel_sum(beg, mid); 
return sum + handle.get(); 
} ! 
int main() 
{ 
std::vector<int> v(10000, 1); 
std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << ‘n'; 
}
Compile-time type identification 
• New header <type_traits> 
• std::is_void<T> 
• std::is_integral<T> 
• std::is_array<T> 
• std::is_class<T> 
• std::is_pointer<T> 
• std::is_volatile<T> 
• std::rank<T>, std::extent<T,N> 
• …. many many more …. 
template <typename R> static const EGType& typeOf() 
{ 
typedef typename std::remove_extent<R>::type A; 
typedef typename std::remove_pointer<R>::type P; 
if (std::is_array<R>::value) { 
typedef typename std::remove_const<A>::type V; 
return arrayType<V>(); 
} else if (std::is_pointer<R>::value) { 
typedef typename std::remove_const<P>::type V; 
return pointerType<V>(); 
} else { 
typedef typename std::remove_const<R>::type V; 
return integralType<V>(); 
} 
}
Variadic templates 
• Variadic templates allow 
implementation of a compile 
time type safe printf(…) 
• Variadic templates combined 
with compile time type 
identification gives super 
powers to C++ 
• Can collect type information 
at compile time and use at 
runtime 
• Allows creation of an 
Objective-C style messaging 
system with late-binding 
#include <type_traits> 
#include <functional> 
#include <string> 
#include <iostream> 
#include <iomanip> ! 
using namespace std; 
using namespace std::placeholders; ! 
template<typename T> 
void reflect_compiler_reflect(T value) 
{ 
cout << setw(70) << typeid(T).name() 
<< " is_pointer=" << is_pointer<T>::value 
<< " is_integral=" << is_integral<T>::value 
<< " is_floating_point=" << is_floating_point<T>::value 
<< " is_class=" << is_class<T>::value 
<< " is_empty=" << is_empty<T>::value 
<< endl; 
} ! 
template<typename T, typename... Args> 
void reflect_compiler_reflect(T value, Args... args) 
{ 
reflect_compiler_reflect(value); 
reflect_compiler_reflect(args...); 
} ! 
int main() 
{ 
int a = 1; 
float b = 4.5f; 
double c[71]; 
string d; 
auto e = [] (int a) {}; 
auto f = std::bind(e, 99, _1); 
struct {} g; 
reflect_compiler_reflect(a, b, c, d, e, f, g); 
}
User-defined literals 
• Allow definition of type 
safe SI units and other 
creative uses 
• Solve Mars Climate 
Orbiter type problems 
class Symbol 
{ 
public: 
string name; 
Symbol(const char *name) : name(name) {} 
}; ! 
Symbol operator "" _symbol(const char* name, size_t) { 
return Symbol(name); 
} ! 
int main() 
{ 
Symbol Foo = "Foo"_symbol; 
}
More functional 
• lambdas 
• [], [=], [&] 
• use in place of old-style function objects 
• combine with existing STL generic algorithms 
e.g. std::partition 
• currying 
• std::function 
• std::bind 
• std::placeholders::_1
Lots more STL 
• Compile time reflection 
• <type_traits> 
• Containers 
• <array>, <unordered_set>, <unordered_map>, <forward_list> 
• Multi-threading 
• <atomic>, <thread>, <mutex>, <future>, <condition_variable> 
• More utility 
• <tuple>, <regex>, <chrono>, <codecvt> 
• Numerics 
• <random>, <ratio>, <cfenv>
C++14 
auto lambda = [](auto x, auto y) {return x + y;}; 
• Generic lambdas 
• lambda initializers 
• Variable templates 
• make_unique 
• Runtime sized arrays 
• Filesystem (Boost::filesystem) 
• Networking 
auto lambda = [value = 1] {return value;}; 
template<typename T> 
constexpr T pi = T(3.1415926535897932385); 
auto u = make_unique<some_type>(ctor, params); 
void foo(size_t n) { int a[n]; }
LLVM / Clang 
• Modular compiler toolchain 
• 2012 ACM Software System Award 
• Clang memory sanitizer 
• Clang address sanitizer 
• Clang thread sanitizer 
• Clang modernize 
• Firefox - emscripten C++ to asm.js 
• Chrome - PNaCl C++ LLVM bitcode 
• Foundation of many OpenCL implementations
C++ - pros 
• Maturity and Performance 
• 30 years of evolution and production use in large scale systems 
• ~ 2x - 3x faster than Java 
• ~ 10x - 100x faster than Ruby and Python 
• Compiler and architecture choices 
• GCC 4.9, Clang 3.4, Intel C++ 14, Visual C++ 2013, etc 
• Easy access to processor features 
• SSE4 Streaming Extensions, AVX-512 SIMD Vector Extensions 
• Compile time static checking and type safety 
• Static analysis tools, valgrind, clang memory sanitizer,… 
• Multi-paradigm 
• Offers a choice of programming styles
C++ - cons 
• Big language, big learning curve 
• Define an idiom with C++: google c++ style 
• Long compile times 
• Idea is to spend time now to save it in the future 
• Missing standard libraries 
• networking, database, graphics, sound, xml and json 
serialization, etc 
• The cost of runtime performance 
• Buffer overflows,Illegal memory accesses 
• Requires static analysis 
• Potential solution: Intel MPX (Memory Protection Extensions)
Conclusion 
• Pick the right language for the job 
• C++ is suitable for infrastructure code where 
performance and type safety are important 
• C++ requires care with memory management 
(leaks, buffer offer-flows, off-by-one errors) 
• Give C++ a go!
Comments / Questions

Más contenido relacionado

La actualidad más candente

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
 
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++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsStephane Gleizes
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++Ilio Catallo
 
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
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMSfawzmasood
 
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
 
Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Olve Maudal
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 

La actualidad más candente (20)

What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
C++11
C++11C++11
C++11
 
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
 
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++ 11C++ 11
C++ 11
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
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)
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
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)
 
Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
C++ references
C++ referencesC++ references
C++ references
 

Similar a Modern C++

Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointersTAlha MAlik
 
Abhishek lingineni
Abhishek lingineniAbhishek lingineni
Abhishek lingineniabhishekl404
 
CPlusPus
CPlusPusCPlusPus
CPlusPusrasen58
 
C++ process new
C++ process newC++ process new
C++ process new敬倫 林
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptTeacherOnat
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptJayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptJayarAlejo
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptEPORI
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptyp02
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.pptInfotech27
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossumoscon2007
 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdfKhaledIbrahim10923
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++kinan keshkeh
 
C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)Christian Nagel
 

Similar a Modern C++ (20)

Return of c++
Return of c++Return of c++
Return of c++
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointers
 
Abhishek lingineni
Abhishek lingineniAbhishek lingineni
Abhishek lingineni
 
CPlusPus
CPlusPusCPlusPus
CPlusPus
 
C++ process new
C++ process newC++ process new
C++ process new
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
C++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.pptC++ programming: Basic introduction to C++.ppt
C++ programming: Basic introduction to C++.ppt
 
C++_programs.ppt
C++_programs.pptC++_programs.ppt
C++_programs.ppt
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossum
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
 
C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)
 

Último

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 

Último (20)

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 

Modern C++

  • 1.
  • 2. Modern C++ (less == more) || (more == more) ! mailto:michael@meta.sg
  • 3. History 1958!!ALGOL!58!!(Bauer,(Backus(et(al) 1957!!FORTRAN!!(John(Backus) 1969!!C!!(Dennis(Ritchie) 1968!!ALGOL!68 1966!!FORTRAN!66 1983!!AT&T!C++!/!Cfront!!(Bjarne(Stroustrup) 1981!!Objec:ve<C!!(Brad(Cox(and(Tom(Love) 1979!!C!with!Classes!!(Bjarne(Stroustrup) 1978!!K&R!C!!(Brian(Kernighan(and(Dennis(Ritchie) 1977!!FORTRAN!77 1989!!ANSI/ISO!C89 1987!!GNU!C!Released 2003!!ISO!C++03 1999!ISO!C99 1998!!ISO!C++98 2017!!ISO!C++17 2014!!ISO!C++14 2011!!ISO!C++11 2008!!Clang!Released 2008!!ISO!C++!TR1 1950 1960 1970 1980 1990 2000 2010 2020
  • 4. What is C++? • A general purpose programming language • Has a strong bias towards systems programming • One layer above assembly language int do_rdrand() { • Multi-paradigm language int r; asm("retry: rdrand eaxn” " jnc retry;n" : "=a" (r)); return r; • Imperative (C99 superset) } • Object-oriented (polymorphism, inheritance) • Functional (immutability, lambdas, currying) • Meta-programming (templates, algorithms)
  • 5. Why C++ • ISO/IEC 14882:2011 • International standards organisation represented by national bodies from 164 member countries • Not open to manipulation by individuals or corporations for proprietary control • COBOL, Fortran, Ada, Prolog, C, C++, Forth, ECMAscript • C++ is a software engineering language that enables the production of high performance type-safe, statically verifiable native software.
  • 6. Why C++ • C++ is popular Source: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
  • 7. C++ projects • GNU Compiler Collection, LLVM, Clang • Java Virtual Machine, Dart, V8, Facebook HHVM • Webkit, Chrome, Safari, Firefox, Internet Explorer • MySQL, LevelDB, MongoDB, SAP DB • Adobe, Autodesk, Apple, Microsoft, … • Scientific computing, Computer Aided Engineering, Operations Research, Telecommunications, … • Bitcoin ☺
  • 8. What is Modern C++? • Modern C++! • feels like “a new language” (Bjarne Stroustrup) • C++11 refines the C++ language in a backwards compatible way to support existing C++ code while enabling simpler and more modern idioms • Pass-by-value everywhere • STL containers got smarter with r-value references • Lambdas, initializer lists, variadic templates, etc • Improved memory management • Improved standard library
  • 9. Less is exponentially more “I was asked a few weeks ago, "What was the biggest surprise you encountered rolling out Go?" I knew the answer instantly: Although we expected C++ programmers to see Go as an alternative, instead most Go programmers come from languages like Python and Ruby. Very few come from C++. We—Ken, Robert and myself—were C++ programmers when we designed a new language to solve the problems that we thought needed to be solved for the kind of software we wrote. It seems almost paradoxical that other C++ programmers don't seem to care.” –Rob Pike http://commandcenter.blogspot.com/2012/06/less-is-exponentially-more.html
  • 10. C++11 has got more std::future auto lambda <functional> std::shared_ptr <thread> std::async <random> <type_traits> <memory> constexpr decltype <regex> <mutex> std::tuple std::unordered_map std::unordered_set nullptr <atomic> static_assert std::unique_ptr <chrono>
  • 11. C++11 has got less C++98 C++11 #include <iostream> #include <vector> ! using namespace std; ! int main() { vector<int> v; v.push_back(2); v.push_back(3); v.push_back(5); v.push_back(7); vector<int>::iterator i; for (i = v.begin(); i != v.end(); i++) { cout << *i << endl; } } #include <iostream> #include <vector> ! using namespace std; ! int main() { vector<int> v = {2,3,5,7}; for (auto a : v) { cout << a << endl; } } or even less int main() { for (auto a : {2,3,5,7}) cout << a << endl; }
  • 12. Template Metaprogramming • The C++ template system is turing complete at compile time #include <iostream> // http://stackoverflow.com/questions/3082113/calculating-factorial-using-template-meta-programming ! using namespace std; ! template <int N> struct Factorial { enum { value = N * Factorial<N - 1>::value }; }; ! template <> struct Factorial<0> { enum { value = 1 }; }; ! int main() { cout << Factorial<4>::value << endl; cout << Factorial<0>::value << endl; }
  • 13. Zero-copy layout OO Language C++11 class Price { int timestamp; int volume; double price; Price(int timestamp, int volume, double price) { this.timestamp = timestamp; this.volume = volume; this.price = price; } static void main(String args[]) { ArrayList<Price> prices; prices.ensureCapacity(4); prices.add(new Price(1307725482, 5, 26.05)); prices.add(new Price(1307725483, 40, 26.15)); prices.add(new Price(1307725493, 100, 26.1499)); prices.add(new Price(1307725493, 112, 26.15)); } } struct Price { int timestamp; int volume; double price; Price(int timestamp, int volume, double price) : timestamp(timestamp), volume(volume), price(price) {} }; ! int main() { vector<Price> prices; prices.reserve(4); prices.emplace_back(1307725482, 5, 26.05); prices.emplace_back(1307725483, 40, 26.15); prices.emplace_back(1307725493, 100, 26.1499); prices.emplace_back(1307725493, 112, 26.15); } • Containers use polymorphism and type erase • Need to devolve to structure of primitive arrays to get reasonable performance, but SoA has bad cache behaviour • Looks are deceiving • Templates instantiate optimized code for the type • Superset - allows implementation of polymorphic containers i.e. vector<object>
  • 14. Zero-copy layout OO Language - 176 bytes, 5 allocations Opaque Object Header (16-bytes) array object reference size length Opaque Object Header (16-bytes) Opaque Object Header (16-bytes) 64-bit Object Reference (8-bytes) 64-bit Object Reference (8-bytes) 64-bit Object Reference (8-bytes) Opaque Object Header (16-bytes) Opaque Object Header (16-bytes) 1307725482 5 26.05 1307725483 40 26.15 1307725493 100 26.1499 C++11 - 72 bytes, 2 allocations array ptr size capacity 1307725482 5 26.05 1307725483 40 26.15 1307725493 100 26.1499
  • 15. No more leaks • std::unique_ptr • Singleton pointer wrapper • Ensures a single copy of an object • No performance overhead • Automatic release (no more delete) • std::shared_ptr • Reference counting pointer wrapper • Thread-safe reference counter • Acts like a normal pointer but has overhead • Use when correctness is more important than performance • Automatic release (no more delete)
  • 16. R-value references • template<typename T> void foo(T&&) • New signature to detect r-values (temporaries) • In a nut shell enables compile time detection of r-value temporaries (versus l-values) to implement efficient zero-copy move semantics and perfect forwarding • What is an r-value? vector<Price> raise(vector<Price> prices) { /* does something */ } raise({Price(1307725482, 5, 26.05), Price(1307725483, 40, 26.15)}); • Adds complexity to hide complexity • Feature designed for STL and library writers • std::forward, std::move • Efficient pass by value and return by value for containers • Simplifies idiom of user code
  • 17. Initializer lists • Array initialization was a C++98 bugbear • Adds new initializer syntax foo{1,2,3,4} • Can be used in constructor member initialization • New header <initializer_list> • New template initializer_list<T> struct myclass { myclass (int,int); myclass (initializer_list<int>); /* definitions ... */ }; ! myclass foo {10,20}; // calls initializer_list ctor myclass bar (10,20); // calls first constructor template <typename T> struct vec { vec(T x) : m{x, 0, 0, 1} {} vec(T x, T y) : m{x, y, 0, 1} {} vec(T x, T y, T z) : m{x, y, z, 1} {} vec(T x, T y, T z, T w) : m{x, y, z, w} {} T m[4]; }; ! vec<float> a(1, 2, 3);
  • 18. Delegating constructors • Really. C++98 didn’t have them template <typename T> struct vec { vec(T x) : vec(x, 0, 0, 1) {} vec(T x, T y) : vec(x, y, 0, 1) {} vec(T x, T y, T z) : vec(x, y, z, 1) {} vec(T x, T y, T z, T w) : m{x, y, z, w} {} T m[4]; };
  • 19. Threads and Futures • std::thread • std::async • std::future • C++ is in now line with other modern languages thread capabilities #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <future> ! // source http://en.cppreference.com/w/cpp/thread/async ! template <typename I> int parallel_sum(I beg, I end) { typename I::difference_type len = end - beg; if (len < 1000) { return std::accumulate(beg, end, 0); } I mid = beg + len/2; auto handle = std::async(std::launch::async, parallel_sum<I>, mid, end); int sum = parallel_sum(beg, mid); return sum + handle.get(); } ! int main() { std::vector<int> v(10000, 1); std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << ‘n'; }
  • 20. Compile-time type identification • New header <type_traits> • std::is_void<T> • std::is_integral<T> • std::is_array<T> • std::is_class<T> • std::is_pointer<T> • std::is_volatile<T> • std::rank<T>, std::extent<T,N> • …. many many more …. template <typename R> static const EGType& typeOf() { typedef typename std::remove_extent<R>::type A; typedef typename std::remove_pointer<R>::type P; if (std::is_array<R>::value) { typedef typename std::remove_const<A>::type V; return arrayType<V>(); } else if (std::is_pointer<R>::value) { typedef typename std::remove_const<P>::type V; return pointerType<V>(); } else { typedef typename std::remove_const<R>::type V; return integralType<V>(); } }
  • 21. Variadic templates • Variadic templates allow implementation of a compile time type safe printf(…) • Variadic templates combined with compile time type identification gives super powers to C++ • Can collect type information at compile time and use at runtime • Allows creation of an Objective-C style messaging system with late-binding #include <type_traits> #include <functional> #include <string> #include <iostream> #include <iomanip> ! using namespace std; using namespace std::placeholders; ! template<typename T> void reflect_compiler_reflect(T value) { cout << setw(70) << typeid(T).name() << " is_pointer=" << is_pointer<T>::value << " is_integral=" << is_integral<T>::value << " is_floating_point=" << is_floating_point<T>::value << " is_class=" << is_class<T>::value << " is_empty=" << is_empty<T>::value << endl; } ! template<typename T, typename... Args> void reflect_compiler_reflect(T value, Args... args) { reflect_compiler_reflect(value); reflect_compiler_reflect(args...); } ! int main() { int a = 1; float b = 4.5f; double c[71]; string d; auto e = [] (int a) {}; auto f = std::bind(e, 99, _1); struct {} g; reflect_compiler_reflect(a, b, c, d, e, f, g); }
  • 22. User-defined literals • Allow definition of type safe SI units and other creative uses • Solve Mars Climate Orbiter type problems class Symbol { public: string name; Symbol(const char *name) : name(name) {} }; ! Symbol operator "" _symbol(const char* name, size_t) { return Symbol(name); } ! int main() { Symbol Foo = "Foo"_symbol; }
  • 23. More functional • lambdas • [], [=], [&] • use in place of old-style function objects • combine with existing STL generic algorithms e.g. std::partition • currying • std::function • std::bind • std::placeholders::_1
  • 24. Lots more STL • Compile time reflection • <type_traits> • Containers • <array>, <unordered_set>, <unordered_map>, <forward_list> • Multi-threading • <atomic>, <thread>, <mutex>, <future>, <condition_variable> • More utility • <tuple>, <regex>, <chrono>, <codecvt> • Numerics • <random>, <ratio>, <cfenv>
  • 25. C++14 auto lambda = [](auto x, auto y) {return x + y;}; • Generic lambdas • lambda initializers • Variable templates • make_unique • Runtime sized arrays • Filesystem (Boost::filesystem) • Networking auto lambda = [value = 1] {return value;}; template<typename T> constexpr T pi = T(3.1415926535897932385); auto u = make_unique<some_type>(ctor, params); void foo(size_t n) { int a[n]; }
  • 26. LLVM / Clang • Modular compiler toolchain • 2012 ACM Software System Award • Clang memory sanitizer • Clang address sanitizer • Clang thread sanitizer • Clang modernize • Firefox - emscripten C++ to asm.js • Chrome - PNaCl C++ LLVM bitcode • Foundation of many OpenCL implementations
  • 27. C++ - pros • Maturity and Performance • 30 years of evolution and production use in large scale systems • ~ 2x - 3x faster than Java • ~ 10x - 100x faster than Ruby and Python • Compiler and architecture choices • GCC 4.9, Clang 3.4, Intel C++ 14, Visual C++ 2013, etc • Easy access to processor features • SSE4 Streaming Extensions, AVX-512 SIMD Vector Extensions • Compile time static checking and type safety • Static analysis tools, valgrind, clang memory sanitizer,… • Multi-paradigm • Offers a choice of programming styles
  • 28. C++ - cons • Big language, big learning curve • Define an idiom with C++: google c++ style • Long compile times • Idea is to spend time now to save it in the future • Missing standard libraries • networking, database, graphics, sound, xml and json serialization, etc • The cost of runtime performance • Buffer overflows,Illegal memory accesses • Requires static analysis • Potential solution: Intel MPX (Memory Protection Extensions)
  • 29. Conclusion • Pick the right language for the job • C++ is suitable for infrastructure code where performance and type safety are important • C++ requires care with memory management (leaks, buffer offer-flows, off-by-one errors) • Give C++ a go!