SlideShare a Scribd company logo
C++11 Style – A Touch of Class
Bjarne Stroustrup
Texas A&M University
www.research.att.com/~bs
Abstract
We know how to write bad code: Litter our programs with casts, macros, pointers, naked
new and deletes, and complicated control structures. Alternatively (or in addition), obscure
every design decision in a mess of deeply nested abstractions using the latest object-
oriented programming and generic programming tricks. For good measure, complicate our
algorithms with interesting special cases. Such code is incomprehensible, unmaintainable,
usually inefficient, and not uncommon.
But how do we write good code? What principles, techniques, and idioms can we exploit to
make it easier to produce quality code? I will make an argument for type-rich interfaces,
compact data structures, integrated resource management and error handling, and highly-
structured algorithmic code. I will illustrate my ideas and motivate my guidelines with a few
idiomatic code examples.
I will use C++11 freely. Examples include auto, general constant expressions, uniform
initialization, type aliases, type safe threading, and user-defined literals. C++ features are
only just starting to appear in production compilers, so some of my suggestions have the
nature of conjecture. However, developing a “modern style” is essential if we don’t want to
maintain newly-written 1970s and 1980s style code in 2020.
This presentation reflects my thoughts on what “Modern C++” should mean in the 2010s: a
language for programming based on light-weight abstraction with a direct and efficient
mapping to hardware, suitable for infrastructure code.
Stroustrup - C++11 Style -
Feb'12
2
What is C++?
A multi-paradigm
programming language
It’s C!
A hybrid language
An object-oriented programming language
Template
meta-programming!
A random collection of features
Embedded systems
programming language
Low level!
Buffer
overflows
Too big!
Generic programming
Stroustrup - C++11 Style -
Feb'12
3
C++
Key strengths:
• software infrastructure
• resource-constrained applications
A light-weight abstraction programming language
Stroustrup - C++11 Style -
Feb'12
4
No one size fits all
• Different projects have different constraints
– Hardware resources
– Reliability constraints
– Efficiency constraints
• Time
• Power
– Time to completion
– Developer skills
• Extremes
– All that matters is to get to the market first!
– If the program fails, people die
– A 50% overhead implies the need for another $50M server farm
Stroustrup - C++11 Style - Feb'12 5
“Multi-paradigm” is not good enough
These styles were never meant to be disjoint:
• C style
– functions and structures
– Typically lots of macros, void*, and casts
• C++85 style (aka “C with Classes”)
– classes, class hierarchies, and virtual functions
• “True OO” style
– Just class hierarchies
– Often lots of casts and macros
• Generic C++
– Everything is a template
Stroustrup - C++11 Style - Feb'12 6
What we want
• Easy to understand
– For humans and tools
– correctness, maintainability
• Modularity
– Well-specified interfaces
– Well-defined error-handling strategy
• Effective Resource management
– Memory, locks, files, …
• Thread safety
– Unless specifically not
• Efficient
– Compact data structures
– Obvious algorithmic structure
• Portable
– Unless specifically not
Stroustrup - C++11 Style - Feb'12 7
What we want
• A synthesis
– And integrated set of features
– C++11 is a significant improvement
• Articulated guidelines for use
– What I call “style”
Stroustrup - C++11 Style - Feb'12 8
Overview
• Ghastly style
– qsort() example
• Type-rich Programming
– Interfaces
– SI example
• Resources and errors
– RAII
– Resource handles and pointers
– Move semantics
• Compact data structures
– List vs. vector
– Vector of point
• Simplify control structure
– Algorithms, lambdas
• Low-level != efficient
• Type-safe concurrency
– Threads, async(), and futures
B. Stroustrup: Software Development for Infrastructure. IEEE Computer, January 2012
Stroustrup - C++11 Style - Feb'12 9
ISO C++11
• This is a talk about how to use C++ well
– In particular, ISO C++11
– C++ features as a whole support programming style
• This is not a talk about the new C++11 features
– I use those where appropriate
– My C++11 FAQ lists the new features
• Most C++11 features are already shipping
– E.g. Clang, GCC, and Microsoft C++
(the order is alphabetical )
• The C++11 standard library is shipping
– E.g. Boost, Clang, GCC, Microsoft C++
Stroustrup - C++11 Style - Feb'12 10
Ghastly Style
void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
void f(char* arr, int m, double* darr, int n)
{
qsort(arr, m, sizeof(char *), cmpstringp);
qsort(darr, n, sizeof(double), compare_double);
}
Stroustrup - C++11 Style - Feb'12 11
Memory to be sorted
Number of elements in memory
Number of bytes in an element
Element comparison function
“It” doesn’t know how to compare doubles?
“It” doesn’t know the size of a double?
“It” doesn’t know the number of elements?
Ghastly Style
void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
static int cmpstringp(const void *p1, const void *p2)
{ /* The actual arguments to this function are "pointers to pointers to char */
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
static int compare_double(const void *p1, const void *p2)
{
double p0 = *(double*)p;
double q0 = *(double*)q;
if (p0 > q0) return 1;
if (p0 < q0) return -1;
return 0;
}
Stroustrup - C++11 Style - Feb'12 12
Throw away useful type information
Use inefficient indirect function call
Prevent inlining
Ghastly Style
• qsort() implementation details
– Note: I looked for implementations of qsort() on the web, but most of what I found were
“educational fakes”
/* Byte-wise swap two items of size SIZE. */
#define SWAP(a, b, size) do { register size_t __size = (size); register char *__a = (a), *__b = (b); do { char __tmp =
*__a; *__a++ = *__b; *__b++ = __tmp; } while (--__size > 0); } while (0)
/* … */
char *mid = lo + size * ((hi - lo) / size >> 1);
if ((*cmp) ((void *) mid, (void *) lo) < 0) SWAP (mid, lo, size);
if ((*cmp) ((void *) hi, (void *) mid) < 0) SWAP (mid, hi, size); else goto jump_over;
if ((*cmp) ((void *) mid, (void *) lo) < 0) SWAP (mid, lo, size);
jump_over:;
Stroustrup - C++11 Style - Feb'12 13
Swap bytes (POD only)
Lots of byte address manipulation
Lots of indirect function calls
Unfair? No!
• I didn’t make up that example
– it is repeatedly offered as an example of good code (for decades)
– qsort() is a popular ISO C standard-library function
– That qsort() code is readable compared to most low-level C/C++ code
• The style is not uncommon in production code
– Teaching and academic versions often simplify to protect the innocent (fraud?)
• I see much worse on bulletin boards
– Have a look, and cry
• Many students aim for that level of code
– “for efficiency”
– because it is cool (their idols does/did it!)
• It’s not just a C/C++ problem/style
– Though I see C and C-style teaching as the source of the problem
Stroustrup - C++11 Style - Feb'12 14
Does it matter? Yes!
• Bad style is the #1 problem in real-world C++ code
– Makes progress relatively easy
– Only relatively: bad code breeds more bad code
• Lack of focus on style is the #1 problem in C++ teaching
– A “quick hack” is usually the quickest short-term solution
• Faster than thinking about “design”
– “They” typically teach poor style
– Many are self-taught
• Take advice from
– Decades old books
– Other novices
• Imitate
– Other languages
– Bad old code
Stroustrup - C++11 Style - Feb'12 15
So what do I want?
• Simple interfaces
void sort(Container&); // for any container (e.g. vector, list, array)
// I can’t quite get this is in C++ (but close)
• Simple calls
vector<string> vs;
// …
sort(vs); // this, I can do
• Uncompromising performance
– Done: std::sort() beats qsort() by large factors (not just a few percent)
• No static type violations
– Done
• No resource leaks
– Done (without a garbage collector)
Stroustrup - C++11 Style - Feb'12 16
Type-rich Programming
• Interfaces
• SI-units
Stroustrup - C++11 Style - Feb'12 17
Focus on interfaces
• Underspecified / overly general:
– void increase_speed(double);
– Object obj; … obj.draw();
– Rectangle(int,int,int,int);
• Better:
– void increase_speed(Speed);
– Shape& s; … s.draw();
– Rectangle(Point top_left, Point bottom_right);
– Rectangle(Point top_left, Box_hw b);
Stroustrup - C++11 Style - Feb'12 18
SI Units
• Units are effective and simple:
Speed sp1 = 100m/9.8s; // very fast for a human
Speed sp2 = 100m/9.8s2; // error (m/s2 is acceleration)
Speed sp3 = 100/9.8s; // error (speed is m/s and 100 has no unit)
Acceleration acc = sp1/0.5s; // too fast for a human
• They are also almost never used in programs
– General-purpose languages generally don’t directly support units
– Run-time checking is far too costly
Stroustrup - C++11 Style - Feb'12 19
SI Units
• We can define Units to be handled at compile time:
template<int M, int K, int S> struct Unit { // a unit in the MKS system
enum { m=M, kg=K, s=S };
};
template<typename Unit> // a magnitude with a unit
struct Value {
double val; // the magnitude
explicit Value(double d) : val(d) {} // construct a Value from a double
};
using Speed = Value<Unit<1,0,-1>>; // meters/second type
using Acceleration = Value<Unit<1,0,-2>>; // meters/second/second type
Stroustrup - C++11 Style - Feb'12 20
SI Units
• We have had libraries like that for a decade
– but people never used them:
Speed sp1 = Value<1,0,0> (100)/ Value<0,0,1> (9.8); // very explicit
Speed sp1 = Value<M> (100)/ Value<S> (9.8); // use a shorthand notation
Speed sp1 = Meters(100)/Seconds(9.8); // abbreviate further still
Speed sp1 = M(100)/S(9.8); // this is getting cryptic
• Notation matters.
Stroustrup - C++11 Style - Feb'12 21
SI Units
• So, improve notation using user-defined literals:
using Second = Unit<0,0,1>; // unit: sec
using Second2 = Unit<0,0,2>; // unit: second*second
constexpr Value<Second> operator"" s(long double d)
// a f-p literal suffixed by ‘s’
{
return Value<Second> (d);
}
constexpr Value<Second2> operator"" s2(long double d)
// a f-p literal suffixed by ‘s2’
{
return Value<Second2> (d);
}
Stroustrup - C++11 Style - Feb'12 22
SI Units
• Units are effective and simple:
Speed sp1 = 100m/9.8s; // very fast for a human
Speed sp2 = 100m/9.8s2; // error (m/s2 is acceleration)
Speed sp3 = 100/9.8s; // error (speed is m/s and 100 has no unit)
Acceleration acc = sp1/0.5s; // too fast for a human
• and essentially free (in C++11)
– Compile-time only
– No run-time overheads
Stroustrup - C++11 Style - Feb'12 23
Style
• Keep interfaces strongly typed
– Avoid very general types in interfaces, e.g.,
• int, double, …
• Object, …
Because such types can represent just about anything
– Checking of trivial types find only trivial errors
– Use precisely specified interfaces
Stroustrup - C++11 Style - Feb'12 24
Resources and Errors
• Resources
• RAII
• Move
Stroustrup - C++11 Style - Feb'12 25
26
Resources and Errors
// unsafe, naĂŻve use:
void f(const char* p)
{
FILE* f = fopen(p,"r"); // acquire
// use f
fclose(f); // release
}
Stroustrup - C++11 Style - Feb'12
27
Resources and Errors
// naĂŻve fix:
void f(const char* p)
{
FILE* f = 0;
try {
f = fopen(p, "r");
// use f
}
catch (…) { // handle every exception
if (f) fclose(f);
throw;
}
if (f) fclose(f);
}
Stroustrup - C++11 Style - Feb'12
28
RAII (Resource Acquisition Is Initialization)
// use an object to represent a resource
class File_handle { // belongs in some support library
FILE* p;
public:
File_handle(const char* pp, const char* r)
{ p = fopen(pp,r); if (p==0) throw File_error{pp,r}; }
File_handle(const string& s, const char* r)
{ p = fopen(s.c_str(),r); if (p==0) throw File_error{s,r}; }
~File_handle() { fclose(p); } // destructor
// copy operations
// access functions
};
void f(string s)
{
File_handle fh {s, "r“};
// use fh
}
Stroustrup - C++11 Style - Feb'12
RAII
• For all resources
– Memory (done by std::string, std::vector, std::map, …)
– Locks (e.g. std::unique_lock), files (e.g. std::fstream), sockets, threads (e.g. std::thread), …
std::mutex m; // a resource
int sh; // shared data
void f()
{
// …
std::unique_lock lck {m}; // grab (acquire) the mutex
sh+=1; // manipulate shared data
} // implicitly release the mutex
Stroustrup - C++11 Style - Feb'12 29
Resource Handles and Pointers
• Many (most?) uses of pointers in local scope are not exception safe
void f(int n, int x)
{
Gadget* p = new Gadget{n}; // look I’m a java programmer! 
// …
if (x<100) throw std::run_time_error{“Weird!”}; // leak
if (x<200) return; // leak
// …
delete p; // and I want my garbage collector! 
}
– No “Naked New”!
– But, why use a “raw” pointer?
Stroustrup - C++11 Style - Feb'12 30
Resource Handles and Pointers
• A std::shared_ptr releases its object at when the last shared_ptr to it is destroyed
void f(int n, int x)
{
shared_ptr<Gadget> p = new Gadget{n}; // manage that pointer!
// …
if (x<100) throw std::run_time_error{“Weird!”}; // no leak
if (x<200) return; // no leak
// …
}
– But why use a shared_ptr?
– I’m not sharing anything.
Stroustrup - C++11 Style - Feb'12 31
Resource Handles and Pointers
• A std::unique_ptr releases its object at when the unique_ptr is destroyed
void f(int n, int x)
{
unique_ptr<Gadget> p = new Gadget{n};
// …
if (x<100) throw std::run_time_error{“Weird!”}; // no leak
if (x<200) return; // no leak
// …
}
Stroustrup - C++11 Style - Feb'12 32
• But why use any kind of pointer ?
• I’m not passing anything around.
Resource Handles and Pointers
• But why use a pointer at all?
• If you can, just use a scoped variable
void f(int n, int x)
{
Gadget g {n};
// …
if (x<100) throw std::run_time_error{“Weird!”}; // no leak
if (x<200) return; // no leak
// …
}
Stroustrup - C++11 Style - Feb'12 33
Resource Management Style
• Prefer classes where the resource management is part of their fundamental semantics
– E.g., std::vector, std::ostream, std::thread, …
• Use “smart pointers” to address the problems of premature destruction and leaks
– std::unique_ptr for (unique) ownership
• Zero cost (time and space)
– std::shared_ptr for shared ownership
• Maintains a use count
– But they are still pointers
• “any pointer is a potential race condition – even in a single threaded program”
Stroustrup - C++11 Style - Feb'12 34
How to move a resource
• Common problem:
– How to get a lot of data cheaply out of a function
• Idea #1:
– Return a pointer to a new’d object
Matrix* operator+(const Matrix&, const Matrix&);
Matrix& res = *(a+b); // ugly! (unacceptable)
– Who does the delete?
• there is no good general answer
Stroustrup - C++11 Style - Feb'12 35
How to move a resource
• Common problem:
– How to get a lot of data cheaply out of a function
• Idea #2
– Return a reference to a new’d object
Matrix& operator+(const Matrix&, const Matrix&);
Matrix res = a+b; // looks right, but …
– Who does the delete?
• What delete? I don’t see any pointers.
• there is no good general answer
Stroustrup - C++11 Style - Feb'12 36
How to move a resource
• Common problem:
– How to get a lot of data cheaply out of a function
• Idea #3
– Pass an reference to a result object
void operator+(const Matrix&, const Matrix&, Matrix& result);
Matrix res = a+b; // Oops, doesn’t work for operators
Matrix res2;
operator+(a,b,res2); // Ugly!
– We are regressing towards assembly code
Stroustrup - C++11 Style - Feb'12 37
How to move a resource
• Common problem:
– How to get a lot of data cheaply out of a function
• Idea #4
– Return a Matrix
Matrix operator+(const Matrix&, const Matrix&);
Matrix res = a+b;
• Copy?
– expensive
• Use some pre-allocated “result stack” of Matrixes
– A brittle hack
• Move the Matrix out
– don’t copy; “steal the representation”
– Directly supported in C++11 through move constructors
Stroustrup - C++11 Style - Feb'12 38
Move semantics
• Return a Matrix
Matrix operator+(const Matrix& a, const Matrix& b)
{
Matrix r;
// copy a[i]+b[i] into r[i] for each i
return r;
}
Matrix res = a+b;
• Define move a constructor for Matrix
– don’t copy; “steal the representation”
Stroustrup - C++11 Style - Feb'12 39
……..
res:
r:
Move semantics
• Direct support in C++11: Move constructor
class Matrix {
Representation rep;
// …
Matrix(Matrix&& a) // move constructor
{
rep = a.rep; // *this gets a’s elements
a.rep = {}; // a becomes the empty Matrix
}
};
Matrix res = a+b;
Stroustrup - C++11 Style - Feb'12 40
……..
res:
r:
Move semantics
• All the standard-library containers have move constructors and move assignments
• vector
• list
• forward_list (singly-linked list)
• map
• unordered_map (hash table)
• set
• …
• string
• Not std::array
Stroustrup - C++11 Style - Feb'12 41
Style
• No naked pointers
– Keep them inside functions and classes
– Keep arrays out of interfaces (prefer containers)
– Pointers are implementation-level artifacts
– A pointer in a function should not represent ownership
– Always consider std::unique_ptr and sometimes std::shared_ptr
• No naked new or delete
– They belong in implementations and as arguments to resource handles
• Return objects “by-value” (using move rather than copy)
– Don’t fiddle with pointer, references, or reference arguments for return values
Stroustrup - C++11 Style - Feb'12 42
Use compact data
• Vector vs. list
• Object layout
Stroustrup - C++11 Style - Feb'12 43
Vector vs. List
• Generate N random integers and insert them into a sequence so that each is
inserted in its proper position in the numerical order. 5 1 4 2 gives:
– 5
– 1 5
– 1 4 5
– 1 2 4 5
• Remove elements one at a time by picking a random position in the sequence and
removing the element there. Positions 1 2 0 0 gives
– 1 2 4 5
– 1 4 5
– 1 4
– 4
• For which N is it better to use a linked list than a vector (or an array) to represent
the sequence?
Stroustrup - C++11 Style - Feb'12 44
Vector vs. List
• Vector beats list massively for insertion and deletion
– For small elements and relatively small numbers (up to 500,000 on my machine)
– Your mileage will vary Stroustrup - C++11 Style - Feb'12 45
0
500
1000
1500
2000
2500
3000
3500
4000
4500
5000
1 2 3 4 5
Seconds
sequence test
vector
list
preallocated list
*100,000 elements
Vector vs. List
• Find the insertion point
– Linear search
– Vector could use binary search, but I did not
• Insert
– List re-links
– Vector moves on average n/2 elements
• Find the deletion point
– Linear search
– Vector could use direct access, but I did not
• delete
– List re-links
– Vector moves on average n/2 elements
• Allocation
– List does N allocations and N deallocations
– The optimized/preallocated list do no allocations or dealloations
– Vector does approximately log2(N) allocations and log2(N) deallocations
– The optimized list does 1 allocation and 1 deallocation
Stroustrup - C++11 Style - Feb'12 46
This completely dominates
Vector vs. List
• The amount of memory used differ dramatically
– List uses 4+ words per element
• it will be worse for 64-bit architectures
• 100,000 list elements take up 6.4MB or more (but I have Gigabytes!?)
– Vector uses 1 word per element
• 100,000 list elements take up 1.6MB or more
• Memory access is relatively slow
– Caches, pipelines, etc.
– 200 to 500 instructions per memory access
– Unpredictable memory access gives many more cache misses
• Implications:
– Don’t store data unnecessarily.
– Keep data compact.
– Access memory in a predictable manner.
Stroustrup - C++11 Style - Feb'12 47
Use compact layout
vector<Point> vp = { Point{1,2}, Point{3,4}, Point{5,6}, Point{7,8} };
Stroustrup - C++11 Style - Feb'12 48
4
2 31 4 6 75 8
1 2
4
65
8743
“True OO” style:
C++:
Simplify control structure
• Prefer algorithms to unstructured code
Stroustrup - C++11 Style - Feb'12 49
Algorithms vs. “Code”
• Problem: drag item to an insertion point
• Original solution (after cleanup and simplification):
– 25 lines of code
• one loop
• three tests
• 14 function calls
• Messy code
– Is it correct?
• who knows? try lots of testing
– Is it maintainable?
• Probably not, since it is hard to understand
– Is it usable elsewhere?
• No, it’s completely hand-crafted to the details of the problem
• The author requested a review
– Professionalism!
Stroustrup - C++11 Style - Feb'12 50
Algorithms vs. “Code”
• Surprise!
– it was a simple find_if followed by moving the item
void drag_item_to(Vector& v, Vector::iterator source, Coordinate p)
{
Vector::iterator dest = find_if(v.begin(), v.end(), contains(p)); // find the insertion point
if (source < dest)
rotate(source, source+1, dest); // from before insertion point
else
rotate(dest, source, source+1); // from after insertion point
}
• It’s comprehensible (maintainable), but still special purpose
– Vector and Coordinate are application specific
Stroustrup - C++11 Style - Feb'12 51
Algorithms vs. “Code”
• Why move only one item?
– Some user interfaces allow you to select many
template < typename Iter, typename Predicate>
pair<Iter, Iter> gather(Iter first, Iter last, Iter p, Predicate pred)
// move elements for which pred() is true to the insertion point p
{
return make_pair(
stable_partition(first, p, !bind(pred, _1)), // from before insertion point
stable_partition(p, last, bind(pred, _1)) // from after insertion point
);
}
• Shorter, simpler, faster, general (usable in many contexts)
– No loops and no tests
Stroustrup - C++11 Style - Feb'12 52
Style
• Focus on algorithms
– Consider generality and re-use
• Consider large functions suspect
• Consider complicated control structures suspect
Stroustrup - C++11 Style - Feb'12 53
Stay high level
• When you can
• Most of the time
Stroustrup - C++11 Style - Feb'12 54
Low-level != efficient
• Language features + compiler + optimizer deliver performance
– You can afford to use libraries of algorithms and types
– for_each()+lambda vs. for-loop
• Examples like these give identical performance on several compilers:
sum = 0;
for(vector<int>::size_type i=0; i<v.size(); ++i) // conventional loop
sum += v[i];
sum = 0;
for_each(v.begin(),v.end(), // algorithm + lambda
[&sum](int x) {sum += x; });
Stroustrup - C++11 Style - Feb'12 55
Low-level != efficient
• Language features + compiler + optimizer deliver performance
– sort() vs. qsort()
– Roughly : C is 2.5 times slower than C++
• Your mileage will wary
• Reasons:
– Type safety
• Transmits more information to the optimizer
• also improves optimization, e.g. type-bases anti-aliasing
– Inlining
• Observations
– Performance of traditional C-style and OO code is roughly equal
– Results vary based on compilers and library implementations
• But sort() is typical
Stroustrup - C++11 Style - Feb'12 56
Low-level != efficient
• Don’t lower your level of abstraction without good reason
• Low-level implies
– More code
– More bugs
– Higher maintenance costs
Stroustrup - C++11 Style - Feb'12 57
Inheritance
Stroustrup - C++11 Style - Feb'12 58
• Use it
– When the domain concepts are hierarchical
– When there is a need for run-time selection among hierarchically ordered alternatives
• Warning:
– Inheritance has been seriously and systematically overused and misused
• “When your only tool is a hammer everything looks like a nail”
Concurrency
• There are many kinds
• Stay high-level
• Stay type-rich
Type-Safe Concurrency
• Programming concurrent systems is hard
– We need all the help we can get
– C++11 offers type-safe programming at the threads-and-locks level
– Type safety is hugely important
• threads-and-locks
– is an unfortunately low level of abstraction
– is necessary for current systems programming
• That’s what the operating systems offer
– presents an abstraction of the hardware to the programmer
– can be the basis of other concurrency abstractions
Stroustrup - C++11 Style - Feb'12 60
Threads
void f(vector<double>&); // function
struct F { // function object
vector<double>& v;
F(vector<double>& vv) :v{vv} { }
void operator()();
};
void code(vector<double>& vec1, vector<double>& vec2)
{
std::thread t1 {f,vec1}; // run f(vec1) on a separate thread
std::thread t2 {F{vec2}}; // run F{vec2}() on a separate thread
t1.join();
t2.join();
// use vec1 and vec2
}
Stroustrup - C++11 Style - Feb'12 61
Thread – pass argument and result
double* f(const vector<double>& v); // read from v return result
double* g(const vector<double>& v); // read from v return result
void user(const vector<double>& some_vec) // note: const
{
double res1, res2;
thread t1 {[&]{ res1 = f(some_vec); }}; // lambda: leave result in res1
thread t2 {[&]{ res2 = g(some_vec); }}; // lambda: leave result in res2
// …
t1.join();
t2.join();
cout << res1 << ' ' << res2 << 'n';
}
Stroustrup - C++11 Style - Feb'12 62
async() – pass argument and return result
double* f(const vector<double>& v); // read from v return result
double* g(const vector<double>& v); // read from v return result
void user(const vector<double>& some_vec) // note: const
{
auto res1 = async(f,some_vec);
auto res2 = async(g,some_vec);
// …
cout << *res1.get() << ' ' << *res2.get() << 'n'; // futures
}
• Much more elegant than the explicit thread version
– And most often faster
Stroustrup - C++11 Style - Feb'12 63
C++ Style
• Practice type-rich programming
– Focus on interfaces
– Simple classes are cheap – use lots of those
– Avoid over-general interfaces
• Integrate Resource Management and Error Handling
– By default, use exceptions and RAII
– Prefer move to complicated pointer use
• Use compact data structures
– By default, use std::vector
• Prefer algorithms to “random code”
• Build and use libraries
– Rely on type-safe concurrency
– By default, start with the ISO C++ standard libraryStroustrup - C++11 Style - Feb'12 64
Questions?
Key strengths:
• software infrastructure
• resource-constrained applications
C++: A light-weight abstraction programming language
Stroustrup - C++11 Style - Feb'12
65

More Related Content

What's hot

State of the Art OpenGL and Qt
State of the Art OpenGL and QtState of the Art OpenGL and Qt
State of the Art OpenGL and Qt
ICS
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
Amar Jukuntla
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
Hossain Md Shakhawat
 
Introduction to gradle
Introduction to gradleIntroduction to gradle
Introduction to gradle
NexThoughts Technologies
 
Pass by value and pass by reference
Pass by value and pass by reference Pass by value and pass by reference
Pass by value and pass by reference
TurnToTech
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
Attila BertĂłk
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ram132
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
Talentica Software
 
Java platform
Java platformJava platform
Java platform
BG Java EE Course
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
Savitribai Phule Pune University
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
ICS
 
Java features
Java featuresJava features
Java featuresmyrajendra
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
ppd1961
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
Thooyavan Venkatachalam
 
Nullable type in C#
Nullable type in C#Nullable type in C#
Nullable type in C#
ZohaibAnwerChaudhry
 
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Abdullah Turkistani
 

What's hot (20)

State of the Art OpenGL and Qt
State of the Art OpenGL and QtState of the Art OpenGL and Qt
State of the Art OpenGL and Qt
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Introduction to gradle
Introduction to gradleIntroduction to gradle
Introduction to gradle
 
Pass by value and pass by reference
Pass by value and pass by reference Pass by value and pass by reference
Pass by value and pass by reference
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
 
Java platform
Java platformJava platform
Java platform
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
 
Java
JavaJava
Java
 
Java features
Java featuresJava features
Java features
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Nullable type in C#
Nullable type in C#Nullable type in C#
Nullable type in C#
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 

Viewers also liked

01 introduction to entity framework
01   introduction to entity framework01   introduction to entity framework
01 introduction to entity framework
Maxim Shaptala
 
Introduction to UML
Introduction to UMLIntroduction to UML
01 introduction to entity framework
01   introduction to entity framework01   introduction to entity framework
01 introduction to entity framework
Марина Босова
 
03 managing relationships
03   managing relationships03   managing relationships
05 managing transactions
05   managing transactions05   managing transactions
04 managing the database
04   managing the database04   managing the database
02 beginning code first
02   beginning code first02   beginning code first
06 integrating extra features and looking forward
06   integrating extra features and looking forward06   integrating extra features and looking forward
06 integrating extra features and looking forward
Марина Босова
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
StĂŠphane BĂŠgaudeau
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
Marc Grabanski
 
001 hosting
001 hosting001 hosting
001 hosting
sivorka
 
000 introduction
000 introduction000 introduction
000 introduction
sivorka
 
Getting started with angular js
Getting started with angular jsGetting started with angular js
Getting started with angular js
Maurice De Beijer [MVP]
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginnersSiva Arunachalam
 
The Art of AngularJS in 2015
The Art of AngularJS in 2015The Art of AngularJS in 2015
The Art of AngularJS in 2015
Matt Raible
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
Stephane Gleizes
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
Geeks Anonymes
 
Web I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolWeb I - 05 - HTTP Protocol
Web I - 05 - HTTP Protocol
Randy Connolly
 
TCP/IP
TCP/IPTCP/IP
Bjarne essencegn13
Bjarne essencegn13Bjarne essencegn13
Bjarne essencegn13Hunde Gurmessa
 

Viewers also liked (20)

01 introduction to entity framework
01   introduction to entity framework01   introduction to entity framework
01 introduction to entity framework
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
01 introduction to entity framework
01   introduction to entity framework01   introduction to entity framework
01 introduction to entity framework
 
03 managing relationships
03   managing relationships03   managing relationships
03 managing relationships
 
05 managing transactions
05   managing transactions05   managing transactions
05 managing transactions
 
04 managing the database
04   managing the database04   managing the database
04 managing the database
 
02 beginning code first
02   beginning code first02   beginning code first
02 beginning code first
 
06 integrating extra features and looking forward
06   integrating extra features and looking forward06   integrating extra features and looking forward
06 integrating extra features and looking forward
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
001 hosting
001 hosting001 hosting
001 hosting
 
000 introduction
000 introduction000 introduction
000 introduction
 
Getting started with angular js
Getting started with angular jsGetting started with angular js
Getting started with angular js
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
The Art of AngularJS in 2015
The Art of AngularJS in 2015The Art of AngularJS in 2015
The Art of AngularJS in 2015
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Web I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolWeb I - 05 - HTTP Protocol
Web I - 05 - HTTP Protocol
 
TCP/IP
TCP/IPTCP/IP
TCP/IP
 
Bjarne essencegn13
Bjarne essencegn13Bjarne essencegn13
Bjarne essencegn13
 

Similar to C++ 11 Style : A Touch of Class

Return of c++
Return of c++Return of c++
Return of c++
Yongwei Wu
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
Tharindu Weerasinghe
 
Peddle the Pedal to the Metal
Peddle the Pedal to the MetalPeddle the Pedal to the Metal
Peddle the Pedal to the Metal
C4Media
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
ssuser0c24d5
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
nilesh405711
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
YashpalYadav46
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
DevliNeeraj
 
Stroustrup c++0x overview
Stroustrup c++0x overviewStroustrup c++0x overview
Stroustrup c++0x overviewVaibhav Bajaj
 
designpatterns_blair_upe.ppt
designpatterns_blair_upe.pptdesignpatterns_blair_upe.ppt
designpatterns_blair_upe.ppt
banti43
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
KurdGul
 
What – if anything – have we learned from C++? by Bjarne Stroustrup @ Curry O...
What – if anything – have we learned from C++? by Bjarne Stroustrup @ Curry O...What – if anything – have we learned from C++? by Bjarne Stroustrup @ Curry O...
What – if anything – have we learned from C++? by Bjarne Stroustrup @ Curry O...
curryon
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
AnassElHousni
 
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Mir Nazim
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
FarazKhan89093
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
Christalin Nelson
 
Week1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC BeginWeek1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC Begin
敬倫 林
 
73d32 session1 c++
73d32 session1 c++73d32 session1 c++
73d32 session1 c++Mukund Trivedi
 
UsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptUsingCPP_for_Artist.ppt
UsingCPP_for_Artist.ppt
vinu28455
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
MZGINBarwary
 
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdfDatabase & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdfInSync2011
 

Similar to C++ 11 Style : A Touch of Class (20)

Return of c++
Return of c++Return of c++
Return of c++
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Peddle the Pedal to the Metal
Peddle the Pedal to the MetalPeddle the Pedal to the Metal
Peddle the Pedal to the Metal
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
Stroustrup c++0x overview
Stroustrup c++0x overviewStroustrup c++0x overview
Stroustrup c++0x overview
 
designpatterns_blair_upe.ppt
designpatterns_blair_upe.pptdesignpatterns_blair_upe.ppt
designpatterns_blair_upe.ppt
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
 
What – if anything – have we learned from C++? by Bjarne Stroustrup @ Curry O...
What – if anything – have we learned from C++? by Bjarne Stroustrup @ Curry O...What – if anything – have we learned from C++? by Bjarne Stroustrup @ Curry O...
What – if anything – have we learned from C++? by Bjarne Stroustrup @ Curry O...
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Week1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC BeginWeek1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC Begin
 
73d32 session1 c++
73d32 session1 c++73d32 session1 c++
73d32 session1 c++
 
UsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptUsingCPP_for_Artist.ppt
UsingCPP_for_Artist.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdfDatabase & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
Database & Technology 1 _ Tom Kyte _ Efficient PL SQL - Why and How to Use.pdf
 

Recently uploaded

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 

Recently uploaded (20)

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 

C++ 11 Style : A Touch of Class

  • 1. C++11 Style – A Touch of Class Bjarne Stroustrup Texas A&M University www.research.att.com/~bs
  • 2. Abstract We know how to write bad code: Litter our programs with casts, macros, pointers, naked new and deletes, and complicated control structures. Alternatively (or in addition), obscure every design decision in a mess of deeply nested abstractions using the latest object- oriented programming and generic programming tricks. For good measure, complicate our algorithms with interesting special cases. Such code is incomprehensible, unmaintainable, usually inefficient, and not uncommon. But how do we write good code? What principles, techniques, and idioms can we exploit to make it easier to produce quality code? I will make an argument for type-rich interfaces, compact data structures, integrated resource management and error handling, and highly- structured algorithmic code. I will illustrate my ideas and motivate my guidelines with a few idiomatic code examples. I will use C++11 freely. Examples include auto, general constant expressions, uniform initialization, type aliases, type safe threading, and user-defined literals. C++ features are only just starting to appear in production compilers, so some of my suggestions have the nature of conjecture. However, developing a “modern style” is essential if we don’t want to maintain newly-written 1970s and 1980s style code in 2020. This presentation reflects my thoughts on what “Modern C++” should mean in the 2010s: a language for programming based on light-weight abstraction with a direct and efficient mapping to hardware, suitable for infrastructure code. Stroustrup - C++11 Style - Feb'12 2
  • 3. What is C++? A multi-paradigm programming language It’s C! A hybrid language An object-oriented programming language Template meta-programming! A random collection of features Embedded systems programming language Low level! Buffer overflows Too big! Generic programming Stroustrup - C++11 Style - Feb'12 3
  • 4. C++ Key strengths: • software infrastructure • resource-constrained applications A light-weight abstraction programming language Stroustrup - C++11 Style - Feb'12 4
  • 5. No one size fits all • Different projects have different constraints – Hardware resources – Reliability constraints – Efficiency constraints • Time • Power – Time to completion – Developer skills • Extremes – All that matters is to get to the market first! – If the program fails, people die – A 50% overhead implies the need for another $50M server farm Stroustrup - C++11 Style - Feb'12 5
  • 6. “Multi-paradigm” is not good enough These styles were never meant to be disjoint: • C style – functions and structures – Typically lots of macros, void*, and casts • C++85 style (aka “C with Classes”) – classes, class hierarchies, and virtual functions • “True OO” style – Just class hierarchies – Often lots of casts and macros • Generic C++ – Everything is a template Stroustrup - C++11 Style - Feb'12 6
  • 7. What we want • Easy to understand – For humans and tools – correctness, maintainability • Modularity – Well-specified interfaces – Well-defined error-handling strategy • Effective Resource management – Memory, locks, files, … • Thread safety – Unless specifically not • Efficient – Compact data structures – Obvious algorithmic structure • Portable – Unless specifically not Stroustrup - C++11 Style - Feb'12 7
  • 8. What we want • A synthesis – And integrated set of features – C++11 is a significant improvement • Articulated guidelines for use – What I call “style” Stroustrup - C++11 Style - Feb'12 8
  • 9. Overview • Ghastly style – qsort() example • Type-rich Programming – Interfaces – SI example • Resources and errors – RAII – Resource handles and pointers – Move semantics • Compact data structures – List vs. vector – Vector of point • Simplify control structure – Algorithms, lambdas • Low-level != efficient • Type-safe concurrency – Threads, async(), and futures B. Stroustrup: Software Development for Infrastructure. IEEE Computer, January 2012 Stroustrup - C++11 Style - Feb'12 9
  • 10. ISO C++11 • This is a talk about how to use C++ well – In particular, ISO C++11 – C++ features as a whole support programming style • This is not a talk about the new C++11 features – I use those where appropriate – My C++11 FAQ lists the new features • Most C++11 features are already shipping – E.g. Clang, GCC, and Microsoft C++ (the order is alphabetical ) • The C++11 standard library is shipping – E.g. Boost, Clang, GCC, Microsoft C++ Stroustrup - C++11 Style - Feb'12 10
  • 11. Ghastly Style void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); void f(char* arr, int m, double* darr, int n) { qsort(arr, m, sizeof(char *), cmpstringp); qsort(darr, n, sizeof(double), compare_double); } Stroustrup - C++11 Style - Feb'12 11 Memory to be sorted Number of elements in memory Number of bytes in an element Element comparison function “It” doesn’t know how to compare doubles? “It” doesn’t know the size of a double? “It” doesn’t know the number of elements?
  • 12. Ghastly Style void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are "pointers to pointers to char */ return strcmp(* (char * const *) p1, * (char * const *) p2); } static int compare_double(const void *p1, const void *p2) { double p0 = *(double*)p; double q0 = *(double*)q; if (p0 > q0) return 1; if (p0 < q0) return -1; return 0; } Stroustrup - C++11 Style - Feb'12 12 Throw away useful type information Use inefficient indirect function call Prevent inlining
  • 13. Ghastly Style • qsort() implementation details – Note: I looked for implementations of qsort() on the web, but most of what I found were “educational fakes” /* Byte-wise swap two items of size SIZE. */ #define SWAP(a, b, size) do { register size_t __size = (size); register char *__a = (a), *__b = (b); do { char __tmp = *__a; *__a++ = *__b; *__b++ = __tmp; } while (--__size > 0); } while (0) /* … */ char *mid = lo + size * ((hi - lo) / size >> 1); if ((*cmp) ((void *) mid, (void *) lo) < 0) SWAP (mid, lo, size); if ((*cmp) ((void *) hi, (void *) mid) < 0) SWAP (mid, hi, size); else goto jump_over; if ((*cmp) ((void *) mid, (void *) lo) < 0) SWAP (mid, lo, size); jump_over:; Stroustrup - C++11 Style - Feb'12 13 Swap bytes (POD only) Lots of byte address manipulation Lots of indirect function calls
  • 14. Unfair? No! • I didn’t make up that example – it is repeatedly offered as an example of good code (for decades) – qsort() is a popular ISO C standard-library function – That qsort() code is readable compared to most low-level C/C++ code • The style is not uncommon in production code – Teaching and academic versions often simplify to protect the innocent (fraud?) • I see much worse on bulletin boards – Have a look, and cry • Many students aim for that level of code – “for efficiency” – because it is cool (their idols does/did it!) • It’s not just a C/C++ problem/style – Though I see C and C-style teaching as the source of the problem Stroustrup - C++11 Style - Feb'12 14
  • 15. Does it matter? Yes! • Bad style is the #1 problem in real-world C++ code – Makes progress relatively easy – Only relatively: bad code breeds more bad code • Lack of focus on style is the #1 problem in C++ teaching – A “quick hack” is usually the quickest short-term solution • Faster than thinking about “design” – “They” typically teach poor style – Many are self-taught • Take advice from – Decades old books – Other novices • Imitate – Other languages – Bad old code Stroustrup - C++11 Style - Feb'12 15
  • 16. So what do I want? • Simple interfaces void sort(Container&); // for any container (e.g. vector, list, array) // I can’t quite get this is in C++ (but close) • Simple calls vector<string> vs; // … sort(vs); // this, I can do • Uncompromising performance – Done: std::sort() beats qsort() by large factors (not just a few percent) • No static type violations – Done • No resource leaks – Done (without a garbage collector) Stroustrup - C++11 Style - Feb'12 16
  • 17. Type-rich Programming • Interfaces • SI-units Stroustrup - C++11 Style - Feb'12 17
  • 18. Focus on interfaces • Underspecified / overly general: – void increase_speed(double); – Object obj; … obj.draw(); – Rectangle(int,int,int,int); • Better: – void increase_speed(Speed); – Shape& s; … s.draw(); – Rectangle(Point top_left, Point bottom_right); – Rectangle(Point top_left, Box_hw b); Stroustrup - C++11 Style - Feb'12 18
  • 19. SI Units • Units are effective and simple: Speed sp1 = 100m/9.8s; // very fast for a human Speed sp2 = 100m/9.8s2; // error (m/s2 is acceleration) Speed sp3 = 100/9.8s; // error (speed is m/s and 100 has no unit) Acceleration acc = sp1/0.5s; // too fast for a human • They are also almost never used in programs – General-purpose languages generally don’t directly support units – Run-time checking is far too costly Stroustrup - C++11 Style - Feb'12 19
  • 20. SI Units • We can define Units to be handled at compile time: template<int M, int K, int S> struct Unit { // a unit in the MKS system enum { m=M, kg=K, s=S }; }; template<typename Unit> // a magnitude with a unit struct Value { double val; // the magnitude explicit Value(double d) : val(d) {} // construct a Value from a double }; using Speed = Value<Unit<1,0,-1>>; // meters/second type using Acceleration = Value<Unit<1,0,-2>>; // meters/second/second type Stroustrup - C++11 Style - Feb'12 20
  • 21. SI Units • We have had libraries like that for a decade – but people never used them: Speed sp1 = Value<1,0,0> (100)/ Value<0,0,1> (9.8); // very explicit Speed sp1 = Value<M> (100)/ Value<S> (9.8); // use a shorthand notation Speed sp1 = Meters(100)/Seconds(9.8); // abbreviate further still Speed sp1 = M(100)/S(9.8); // this is getting cryptic • Notation matters. Stroustrup - C++11 Style - Feb'12 21
  • 22. SI Units • So, improve notation using user-defined literals: using Second = Unit<0,0,1>; // unit: sec using Second2 = Unit<0,0,2>; // unit: second*second constexpr Value<Second> operator"" s(long double d) // a f-p literal suffixed by ‘s’ { return Value<Second> (d); } constexpr Value<Second2> operator"" s2(long double d) // a f-p literal suffixed by ‘s2’ { return Value<Second2> (d); } Stroustrup - C++11 Style - Feb'12 22
  • 23. SI Units • Units are effective and simple: Speed sp1 = 100m/9.8s; // very fast for a human Speed sp2 = 100m/9.8s2; // error (m/s2 is acceleration) Speed sp3 = 100/9.8s; // error (speed is m/s and 100 has no unit) Acceleration acc = sp1/0.5s; // too fast for a human • and essentially free (in C++11) – Compile-time only – No run-time overheads Stroustrup - C++11 Style - Feb'12 23
  • 24. Style • Keep interfaces strongly typed – Avoid very general types in interfaces, e.g., • int, double, … • Object, … Because such types can represent just about anything – Checking of trivial types find only trivial errors – Use precisely specified interfaces Stroustrup - C++11 Style - Feb'12 24
  • 25. Resources and Errors • Resources • RAII • Move Stroustrup - C++11 Style - Feb'12 25
  • 26. 26 Resources and Errors // unsafe, naĂŻve use: void f(const char* p) { FILE* f = fopen(p,"r"); // acquire // use f fclose(f); // release } Stroustrup - C++11 Style - Feb'12
  • 27. 27 Resources and Errors // naĂŻve fix: void f(const char* p) { FILE* f = 0; try { f = fopen(p, "r"); // use f } catch (…) { // handle every exception if (f) fclose(f); throw; } if (f) fclose(f); } Stroustrup - C++11 Style - Feb'12
  • 28. 28 RAII (Resource Acquisition Is Initialization) // use an object to represent a resource class File_handle { // belongs in some support library FILE* p; public: File_handle(const char* pp, const char* r) { p = fopen(pp,r); if (p==0) throw File_error{pp,r}; } File_handle(const string& s, const char* r) { p = fopen(s.c_str(),r); if (p==0) throw File_error{s,r}; } ~File_handle() { fclose(p); } // destructor // copy operations // access functions }; void f(string s) { File_handle fh {s, "r“}; // use fh } Stroustrup - C++11 Style - Feb'12
  • 29. RAII • For all resources – Memory (done by std::string, std::vector, std::map, …) – Locks (e.g. std::unique_lock), files (e.g. std::fstream), sockets, threads (e.g. std::thread), … std::mutex m; // a resource int sh; // shared data void f() { // … std::unique_lock lck {m}; // grab (acquire) the mutex sh+=1; // manipulate shared data } // implicitly release the mutex Stroustrup - C++11 Style - Feb'12 29
  • 30. Resource Handles and Pointers • Many (most?) uses of pointers in local scope are not exception safe void f(int n, int x) { Gadget* p = new Gadget{n}; // look I’m a java programmer!  // … if (x<100) throw std::run_time_error{“Weird!”}; // leak if (x<200) return; // leak // … delete p; // and I want my garbage collector!  } – No “Naked New”! – But, why use a “raw” pointer? Stroustrup - C++11 Style - Feb'12 30
  • 31. Resource Handles and Pointers • A std::shared_ptr releases its object at when the last shared_ptr to it is destroyed void f(int n, int x) { shared_ptr<Gadget> p = new Gadget{n}; // manage that pointer! // … if (x<100) throw std::run_time_error{“Weird!”}; // no leak if (x<200) return; // no leak // … } – But why use a shared_ptr? – I’m not sharing anything. Stroustrup - C++11 Style - Feb'12 31
  • 32. Resource Handles and Pointers • A std::unique_ptr releases its object at when the unique_ptr is destroyed void f(int n, int x) { unique_ptr<Gadget> p = new Gadget{n}; // … if (x<100) throw std::run_time_error{“Weird!”}; // no leak if (x<200) return; // no leak // … } Stroustrup - C++11 Style - Feb'12 32 • But why use any kind of pointer ? • I’m not passing anything around.
  • 33. Resource Handles and Pointers • But why use a pointer at all? • If you can, just use a scoped variable void f(int n, int x) { Gadget g {n}; // … if (x<100) throw std::run_time_error{“Weird!”}; // no leak if (x<200) return; // no leak // … } Stroustrup - C++11 Style - Feb'12 33
  • 34. Resource Management Style • Prefer classes where the resource management is part of their fundamental semantics – E.g., std::vector, std::ostream, std::thread, … • Use “smart pointers” to address the problems of premature destruction and leaks – std::unique_ptr for (unique) ownership • Zero cost (time and space) – std::shared_ptr for shared ownership • Maintains a use count – But they are still pointers • “any pointer is a potential race condition – even in a single threaded program” Stroustrup - C++11 Style - Feb'12 34
  • 35. How to move a resource • Common problem: – How to get a lot of data cheaply out of a function • Idea #1: – Return a pointer to a new’d object Matrix* operator+(const Matrix&, const Matrix&); Matrix& res = *(a+b); // ugly! (unacceptable) – Who does the delete? • there is no good general answer Stroustrup - C++11 Style - Feb'12 35
  • 36. How to move a resource • Common problem: – How to get a lot of data cheaply out of a function • Idea #2 – Return a reference to a new’d object Matrix& operator+(const Matrix&, const Matrix&); Matrix res = a+b; // looks right, but … – Who does the delete? • What delete? I don’t see any pointers. • there is no good general answer Stroustrup - C++11 Style - Feb'12 36
  • 37. How to move a resource • Common problem: – How to get a lot of data cheaply out of a function • Idea #3 – Pass an reference to a result object void operator+(const Matrix&, const Matrix&, Matrix& result); Matrix res = a+b; // Oops, doesn’t work for operators Matrix res2; operator+(a,b,res2); // Ugly! – We are regressing towards assembly code Stroustrup - C++11 Style - Feb'12 37
  • 38. How to move a resource • Common problem: – How to get a lot of data cheaply out of a function • Idea #4 – Return a Matrix Matrix operator+(const Matrix&, const Matrix&); Matrix res = a+b; • Copy? – expensive • Use some pre-allocated “result stack” of Matrixes – A brittle hack • Move the Matrix out – don’t copy; “steal the representation” – Directly supported in C++11 through move constructors Stroustrup - C++11 Style - Feb'12 38
  • 39. Move semantics • Return a Matrix Matrix operator+(const Matrix& a, const Matrix& b) { Matrix r; // copy a[i]+b[i] into r[i] for each i return r; } Matrix res = a+b; • Define move a constructor for Matrix – don’t copy; “steal the representation” Stroustrup - C++11 Style - Feb'12 39 …….. res: r:
  • 40. Move semantics • Direct support in C++11: Move constructor class Matrix { Representation rep; // … Matrix(Matrix&& a) // move constructor { rep = a.rep; // *this gets a’s elements a.rep = {}; // a becomes the empty Matrix } }; Matrix res = a+b; Stroustrup - C++11 Style - Feb'12 40 …….. res: r:
  • 41. Move semantics • All the standard-library containers have move constructors and move assignments • vector • list • forward_list (singly-linked list) • map • unordered_map (hash table) • set • … • string • Not std::array Stroustrup - C++11 Style - Feb'12 41
  • 42. Style • No naked pointers – Keep them inside functions and classes – Keep arrays out of interfaces (prefer containers) – Pointers are implementation-level artifacts – A pointer in a function should not represent ownership – Always consider std::unique_ptr and sometimes std::shared_ptr • No naked new or delete – They belong in implementations and as arguments to resource handles • Return objects “by-value” (using move rather than copy) – Don’t fiddle with pointer, references, or reference arguments for return values Stroustrup - C++11 Style - Feb'12 42
  • 43. Use compact data • Vector vs. list • Object layout Stroustrup - C++11 Style - Feb'12 43
  • 44. Vector vs. List • Generate N random integers and insert them into a sequence so that each is inserted in its proper position in the numerical order. 5 1 4 2 gives: – 5 – 1 5 – 1 4 5 – 1 2 4 5 • Remove elements one at a time by picking a random position in the sequence and removing the element there. Positions 1 2 0 0 gives – 1 2 4 5 – 1 4 5 – 1 4 – 4 • For which N is it better to use a linked list than a vector (or an array) to represent the sequence? Stroustrup - C++11 Style - Feb'12 44
  • 45. Vector vs. List • Vector beats list massively for insertion and deletion – For small elements and relatively small numbers (up to 500,000 on my machine) – Your mileage will vary Stroustrup - C++11 Style - Feb'12 45 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 1 2 3 4 5 Seconds sequence test vector list preallocated list *100,000 elements
  • 46. Vector vs. List • Find the insertion point – Linear search – Vector could use binary search, but I did not • Insert – List re-links – Vector moves on average n/2 elements • Find the deletion point – Linear search – Vector could use direct access, but I did not • delete – List re-links – Vector moves on average n/2 elements • Allocation – List does N allocations and N deallocations – The optimized/preallocated list do no allocations or dealloations – Vector does approximately log2(N) allocations and log2(N) deallocations – The optimized list does 1 allocation and 1 deallocation Stroustrup - C++11 Style - Feb'12 46 This completely dominates
  • 47. Vector vs. List • The amount of memory used differ dramatically – List uses 4+ words per element • it will be worse for 64-bit architectures • 100,000 list elements take up 6.4MB or more (but I have Gigabytes!?) – Vector uses 1 word per element • 100,000 list elements take up 1.6MB or more • Memory access is relatively slow – Caches, pipelines, etc. – 200 to 500 instructions per memory access – Unpredictable memory access gives many more cache misses • Implications: – Don’t store data unnecessarily. – Keep data compact. – Access memory in a predictable manner. Stroustrup - C++11 Style - Feb'12 47
  • 48. Use compact layout vector<Point> vp = { Point{1,2}, Point{3,4}, Point{5,6}, Point{7,8} }; Stroustrup - C++11 Style - Feb'12 48 4 2 31 4 6 75 8 1 2 4 65 8743 “True OO” style: C++:
  • 49. Simplify control structure • Prefer algorithms to unstructured code Stroustrup - C++11 Style - Feb'12 49
  • 50. Algorithms vs. “Code” • Problem: drag item to an insertion point • Original solution (after cleanup and simplification): – 25 lines of code • one loop • three tests • 14 function calls • Messy code – Is it correct? • who knows? try lots of testing – Is it maintainable? • Probably not, since it is hard to understand – Is it usable elsewhere? • No, it’s completely hand-crafted to the details of the problem • The author requested a review – Professionalism! Stroustrup - C++11 Style - Feb'12 50
  • 51. Algorithms vs. “Code” • Surprise! – it was a simple find_if followed by moving the item void drag_item_to(Vector& v, Vector::iterator source, Coordinate p) { Vector::iterator dest = find_if(v.begin(), v.end(), contains(p)); // find the insertion point if (source < dest) rotate(source, source+1, dest); // from before insertion point else rotate(dest, source, source+1); // from after insertion point } • It’s comprehensible (maintainable), but still special purpose – Vector and Coordinate are application specific Stroustrup - C++11 Style - Feb'12 51
  • 52. Algorithms vs. “Code” • Why move only one item? – Some user interfaces allow you to select many template < typename Iter, typename Predicate> pair<Iter, Iter> gather(Iter first, Iter last, Iter p, Predicate pred) // move elements for which pred() is true to the insertion point p { return make_pair( stable_partition(first, p, !bind(pred, _1)), // from before insertion point stable_partition(p, last, bind(pred, _1)) // from after insertion point ); } • Shorter, simpler, faster, general (usable in many contexts) – No loops and no tests Stroustrup - C++11 Style - Feb'12 52
  • 53. Style • Focus on algorithms – Consider generality and re-use • Consider large functions suspect • Consider complicated control structures suspect Stroustrup - C++11 Style - Feb'12 53
  • 54. Stay high level • When you can • Most of the time Stroustrup - C++11 Style - Feb'12 54
  • 55. Low-level != efficient • Language features + compiler + optimizer deliver performance – You can afford to use libraries of algorithms and types – for_each()+lambda vs. for-loop • Examples like these give identical performance on several compilers: sum = 0; for(vector<int>::size_type i=0; i<v.size(); ++i) // conventional loop sum += v[i]; sum = 0; for_each(v.begin(),v.end(), // algorithm + lambda [&sum](int x) {sum += x; }); Stroustrup - C++11 Style - Feb'12 55
  • 56. Low-level != efficient • Language features + compiler + optimizer deliver performance – sort() vs. qsort() – Roughly : C is 2.5 times slower than C++ • Your mileage will wary • Reasons: – Type safety • Transmits more information to the optimizer • also improves optimization, e.g. type-bases anti-aliasing – Inlining • Observations – Performance of traditional C-style and OO code is roughly equal – Results vary based on compilers and library implementations • But sort() is typical Stroustrup - C++11 Style - Feb'12 56
  • 57. Low-level != efficient • Don’t lower your level of abstraction without good reason • Low-level implies – More code – More bugs – Higher maintenance costs Stroustrup - C++11 Style - Feb'12 57
  • 58. Inheritance Stroustrup - C++11 Style - Feb'12 58 • Use it – When the domain concepts are hierarchical – When there is a need for run-time selection among hierarchically ordered alternatives • Warning: – Inheritance has been seriously and systematically overused and misused • “When your only tool is a hammer everything looks like a nail”
  • 59. Concurrency • There are many kinds • Stay high-level • Stay type-rich
  • 60. Type-Safe Concurrency • Programming concurrent systems is hard – We need all the help we can get – C++11 offers type-safe programming at the threads-and-locks level – Type safety is hugely important • threads-and-locks – is an unfortunately low level of abstraction – is necessary for current systems programming • That’s what the operating systems offer – presents an abstraction of the hardware to the programmer – can be the basis of other concurrency abstractions Stroustrup - C++11 Style - Feb'12 60
  • 61. Threads void f(vector<double>&); // function struct F { // function object vector<double>& v; F(vector<double>& vv) :v{vv} { } void operator()(); }; void code(vector<double>& vec1, vector<double>& vec2) { std::thread t1 {f,vec1}; // run f(vec1) on a separate thread std::thread t2 {F{vec2}}; // run F{vec2}() on a separate thread t1.join(); t2.join(); // use vec1 and vec2 } Stroustrup - C++11 Style - Feb'12 61
  • 62. Thread – pass argument and result double* f(const vector<double>& v); // read from v return result double* g(const vector<double>& v); // read from v return result void user(const vector<double>& some_vec) // note: const { double res1, res2; thread t1 {[&]{ res1 = f(some_vec); }}; // lambda: leave result in res1 thread t2 {[&]{ res2 = g(some_vec); }}; // lambda: leave result in res2 // … t1.join(); t2.join(); cout << res1 << ' ' << res2 << 'n'; } Stroustrup - C++11 Style - Feb'12 62
  • 63. async() – pass argument and return result double* f(const vector<double>& v); // read from v return result double* g(const vector<double>& v); // read from v return result void user(const vector<double>& some_vec) // note: const { auto res1 = async(f,some_vec); auto res2 = async(g,some_vec); // … cout << *res1.get() << ' ' << *res2.get() << 'n'; // futures } • Much more elegant than the explicit thread version – And most often faster Stroustrup - C++11 Style - Feb'12 63
  • 64. C++ Style • Practice type-rich programming – Focus on interfaces – Simple classes are cheap – use lots of those – Avoid over-general interfaces • Integrate Resource Management and Error Handling – By default, use exceptions and RAII – Prefer move to complicated pointer use • Use compact data structures – By default, use std::vector • Prefer algorithms to “random code” • Build and use libraries – Rely on type-safe concurrency – By default, start with the ISO C++ standard libraryStroustrup - C++11 Style - Feb'12 64
  • 65. Questions? Key strengths: • software infrastructure • resource-constrained applications C++: A light-weight abstraction programming language Stroustrup - C++11 Style - Feb'12 65