You are on page 1of 55

2-661

2-661
Updates: ISO C++, VC++ roadmap
C++98 C++03 C++11 C++14 C++17
(major) (TC, bug fixes only) (major) (minor) (major)

Lib Fundamentals TS + more (modules, …)


Library TR (aka TS)
Tx Memory TS
Performance TR
Concepts TS
Parallelism TS
Explicit conversion Non-static data __func__ Thread-safe function constexpr (incl.
Inline namespaces
operators member initializers Extended sizeof local static init ctors, literal types)

Implicit move alignof Universal character


Raw string literals = default Expression SFINAE
generation alignas names in literals

Function template Ref-qualifiers: noexcept noexcept C++11 preprocessor


= delete
default arguments & and && for *this (unconditional) (incl. conditional) (incl. C++98 C11)

“using” aliases char16_t, char32_t C++98 two-phase


Delegating constructors C++14 decltype(auto) Inheriting constructors
C++14 libs: type aliases Attributes lookup

Uniform init & C99 variable decls C++14 auto function C++14 generalized
User-defined literals thread_local
initializer_lists C99 _Bool return type deduction constexpr

C++14 generic C++14 generalized C++14 variable


Variadic templates C99 compound literals Unrestricted unions
lambdas (partial) lambda capture templates

C++14 libs: cbegin/ C99 designated C++TS? async/await C++14 libs: std:: user- constexpr (except C++TS concepts
greater<>/make_unique initializers (partial) defined literals ctors, literal types) lite
High confidence

Med confidence

Explicit conversion Non-static data __func__ Thread-safe function constexpr (incl.


Inline namespaces
operators member initializers Extended sizeof local static init ctors, literal types)

Implicit move alignof Universal character


Raw string literals = default Expression SFINAE
generation alignas names in literals

Function template Ref-qualifiers: noexcept noexcept C++11 preprocessor


= delete
default arguments & and && for *this (unconditional) (incl. conditional) (incl. C++98 C11)

“using” aliases char16_t, char32_t C++98 two-phase


Delegating constructors C++14 decltype(auto) Inheriting constructors
C++14 libs: type aliases Attributes lookup

Uniform init & C99 variable decls C++14 auto function C++14 generalized
User-defined literals thread_local
initializer_lists C99 _Bool return type deduction constexpr

C++14 generic C++14 generalized C++14 variable


Variadic templates C99 compound literals Unrestricted unions
lambdas (partial) lambda capture templates

C++14 libs: cbegin/ C99 designated C++TS? async/await C++14 libs: std:: user- constexpr (except C++TS concepts
greater<>/make_unique initializers (partial) defined literals ctors, literal types) lite
Modern C++: What you need to know

C++









▪ ▪

Then: C++98 code
circle* p = new circle( 42 );
vector<shape*> v = load_shapes();
for( vector<shape*>::iteratori = v.begin(); i != v.end(); ++i ) {
if( *i && **i == *p )
cout << **i << “ is a match\n”;
}
// … later, possibly elsewhere …
for( vector<shape*>::iterator i = v.begin();
i != v.end(); ++i ) {
delete *i;
}
delete p;
T* → shared_ptr<T>
auto type deduction
new → make_unique
Then: C++98 code Now: Modern C++ or make_shared

circle* p = new circle( 42 ); auto p = make_shared<circle>( 42 );


vector<shape*> v = load_shapes(); auto v = load_shapes();
for( vector<shape*>::iteratori = v.begin(); i != v.end(); ++i ) { for( auto& s : v ) { range-for
if( *i && **i == *p ) if( s && *s == *p )
cout << **i << “ is a match\n”; cout << *s << “ is a match\n”;
} }
// … later, possibly elsewhere …
for( vector<shape*>::iterator i = v.begin();
i != v.end(); ++i ) {
delete *i;
} not exception-safe no need for “delete” –
delete p; missing try/catch, automatic lifetime management
__try/__finally exception-safe
Python C++14 + concepts using a concept
(note: not yet VC++)

def mean(seq): auto mean(const Sequence& seq) {


n = 0.0 auto n = 0.0;
for x in seq: for (auto x : seq)
n += x n += x;
return n / len(seq) return n / seq.size();
}

automatic return
type deduction
set<widget> load_huge_data() { Matrix operator+(
set<widget> ret; const Matrix&,
// … load data and populate ret … const Matrix&
return ret; );
} m4 = m1+m2+m3;
widgets = load_huge_data();
efficient, no deep copies
efficient, no deep copy

clean semantics of value types


+
efficiency of reference types

if (auto x = …) {

}
int do_work() {
auto x = …;
}
class widget {
gadget g;
};
class widget { void f() { lifetime automatically
“all types are widget w;
private: tied to enclosing scope
IDisposable” :::
gadget g; constructs w, including
lifetime automatically w.draw(); the w.g gadget member
public: tied to enclosing object
void draw(); :::
no leak, exception safe
}; } automatic destruction
and deallocation for w
and w.g
automatic propagation,
as if “widget.dispose()
{ g.dispose(); }” automatic exception
safety, as if “finally {
w.g.dispose();
w.dispose(); }”







{
widget a;
gadget b;
vector<int> c;
:::
}







stack heap heap

ptr
ptr
ptr
stack heap heap stack heap

ptr
ptr
ptr
stack heap heap stack heap stack

ptr
ptr
ptr






array, vector ▪

list, tree, graph, … ▪

You can see it trying to
prefetch, scrambling ▪
madly to stay ahead and
keep the pipeline full…!





▪ http://gameprogrammingpatterns.com/data-locality.html
Original code
class GameEntity {
AIComponent* ai_;
PhysicsComponent* physics_;
RenderComponent* render_;
:::
};
vector<GameEntity> entities;

for(auto& e : entities) { e->ai()->update(); }


for(auto& e : entities) { e->physics()->update(); }
for(auto& e : entities) { e->render()->update(); }
::: // other game loop machinery
Original code Fifty times faster Note: GameEntity is
class GameEntity { class GameEntity { undisturbed
AIComponent* ai_; AIComponent* ai_;
PhysicsComponent* physics_; PhysicsComponent* physics_;
RenderComponent* render_; RenderComponent* render_;
::: ::: Just storing
}; };
the objects in a
vector<GameEntity> entities; vector<GameEntity> entities; different order (!)
vector<AIComponent> cai;
vector<PhysicsComponent> cphysics
vector<RenderComponent> crender;
for(auto& e : entities) { e->ai()->update(); } for(auto& e : cai) { e.update(); }
for(auto& e : entities) { e->physics()->update(); } for(auto& e : cphysics) { e.update(); }
for(auto& e : entities) { e->render()->update(); } for(auto& e : crender) { e.update(); }
::: // other game loop machinery ::: // other game loop machinery
class GameEntity { class GameEntity {
AIComponent* ai_; AIComponent* ai_;
PhysicsComponent* physics_; PhysicsComponent* physics_;
RenderComponent* render_; RenderComponent* render_;
::: :::
}; };
vector<GameEntity> entities; vector<GameEntity> entities;
vector<AIComponent> cai;
vector<PhysicsComponent> cphysics
vector<RenderComponent> crender;
for(auto& e : entities) { e->ai()->update(); } for(auto& e : cai) { e.update(); }
for(auto& e : entities) { e->physics()->update(); } for(auto& e : cphysics) { e.update(); }
for(auto& e : entities) { e->render()->update(); } for(auto& e : crender) { e.update(); }
::: // other game loop machinery ::: // other game loop machinery

Free store info

data









sequence test
5000

4500

4000

3500

3000

Seconds 2500
vector

2000 list

1500
preallocated list
1000

500

0
1 2 3 4 5
*100,000 elements




Free store info



data





▪ This completely dominates



▪ ▪
▪ ▪
▪ ▪

4500

4000

3500

3000
vector
seconds

2500
list
2000
list (preallocate)
1500 map
1000

500

0
1 2 3 4 5







▪ vector< unique_ptr<shape> > shapes;


▪ vector< shape* > shapes;


for/while/do →
std:: algorithms
Then Now [&] lambda functions

for( auto i = v.begin(); i != v.end(); ++i ) { for_each( begin(v), end(v), []( string& s ) {
::: :::
for_each to visit each element
::: :::
::: arbitrary length lambda :::
bodies, just put the loop
} } );
body inside the lambda
find_if to find a match

auto i = v.begin(); auto i = find_if( begin(v), end(v),


for( ; i != v.end(); ++i ) { [](int i) { return i > x && i < y; } );
if (*i > x && *i < y) break;
}


string s = ... ;
int num_words =
inner_product( vec, // in parallel (incl. vector lanes)
begin(s), end(s)-1, begin(s)+1, // for every pair of chars
0, plus<>(), // start at 0 and add 1
[](char left, char right) { return isspace(left) && !isspace(right); }
); // every time we start a word
if( !isspace(s[0]) ) ++num_words;

https://parallelstl.codeplex.com
What you need to know next
▪ 2014-15: Multiple C++14 complete implementations



C++’s biggest conference ever
▪ Sold out, 18 countries, 23 states
▪ Global live webcast: Over 20,000 viewers
▪ Global on demand: Over 600,000 viewers
And we did it again!
▪ Sold out, 10 countries, 26 states
▪ Global live webcast: Over 20,000 viewers
▪ Global on demand: Over 1,000,000 viewers
▪ CppCon 2014 estimate: 150+ full- and half-length talks
▪ Invited talks/panels and domain experts
▪ + Lightning talks
▪ + Evening events and “unconference” time
Call for Submissions open now:

libraries & functional parallelism & C++11 &


frameworks programming multiprocessing C++14

concepts & real-world app high-perf &


generic experience low-latency
programming reports computing

industry-specific (mobile, embedded, tools &


game, trading, scientific, robotics, …) processes

& more …

You might also like