SlideShare une entreprise Scribd logo
1  sur  64
The Essence of C++
with examples in C++84, C++98, C++11, and C++14

Bjarne Stroustrup
Texas A&M University
www.stroustrup.com
Overview
•
•
•
•

Aims and constraints
C++ in four slides
Resource management
OOP: Classes and Hierarchies
– (very briefly)

• GP: Templates
– Requirements checking

• Challenges

Stroustrup - Essence - Going Native'13

3
What did/do I want?
• Type safety
– Encapsulate necessary unsafe operations

• Resource safety
– It’s not all memory

• Performance
– For some parts of almost all systems, it’s important

• Predictability
– For hard and soft real time

• Teachability
– Complexity of code should be proportional to the complexity of the task

• Readability
– People and machines (“analyzability”)
Stroustrup - Essence - Going Native'13

4
Who did/do I want it for?
• Primary concerns
–
–
–
–

Systems programming
Embedded systems
Resource constrained systems
Large systems

• Experts
– “C++ is expert friendly”

• Novices
– C++ Is not just expert friendly

Stroustrup - Essence - Going Native'13

5
Template
meta-programming!

What is C++?

Class hierarchies

A hybrid language
A multi-paradigm
programming language

Buffer
overflows

It’s C!

Classes
Embedded systems
programming language

Too big!

An object-oriented
programming language

Generic programming

Stroustrup - Essence - Going Native'13

Low level!
A random collection
of features
6
C++
A light-weight abstraction
programming language

Key strengths:
• software infrastructure
• resource-constrained applications
Stroustrup - Essence - Going Native'13

7
Programming Languages
Domain-specific
abstraction

General-purpose abstraction

Fortran
Cobol

Simula

Java

C++

C++11

Direct mapping to
hardware

Assembler

BCPL

C

Stroustrup - Essence - Going Native'13

C#

8
What does C++ offer?
• Not perfection
– Of course

• Not everything for everybody
– Of course

• A solid fundamental model
– Yes, really

• 30+ years of real-world “refinement”
– It works

• Performance
– A match for anything

• The best is buried in “compatibility stuff’’
– long-term stability is a feature

Stroustrup - Essence - Going Native'13

9
What does C++ offer?
• C++ in Four slides
–
–
–
–

Map to hardware
Classes
Inheritance
Parameterized types

• If you understand int and vector, you understand C++
– The rest is “details” (1,300+ pages of details)
Stroustrup - Essence - Going Native'13

10
Map to Hardware
• Primitive operations => instructions
– +, %, ->, [], (), …
value

• int, double, complex<double>, Date, …

handle

• vector, string, thread, Matrix, …
value

• Objects can be composed by simple concatenation:
– Arrays
– Classes/structs

value

handle

value

handle

value
value

Stroustrup - Essence - Going Native'13

11
Classes: Construction/Destruction
• From the first week of “C with Classes” (1979)
class X {
// user-defined type
public:
// interface
X(Something); // constructor from Something
~X();
// destructor
// …
private:
// implementation
// …
};

“A constructor establishes the environment for the members to
run in; the destructor reverses its actions.”
Stroustrup - Essence - Going Native'13

12
Abstract Classes and Inheritance
• Insulate the user from the implementation
struct Device {
virtual int put(const char*) = 0;
virtual int get(const char*) = 0;
};

// abstract class
// pure virtual function

• No data members, all data in derived classes
– “not brittle”

• Manipulate through pointer or reference
– Typically allocated on the free store (“dynamic memory”)
– Typically requires some form of lifetime management (use resource
handles)

• Is the root of a hierarchy of derived classes
Stroustrup - Essence - Going Native'13

13
Parameterized Types and Classes
• Templates
– Essential: Support for generic programming
– Secondary: Support for compile-time computation
template<typename T>
class vector { /* … */ };

// a generic type

vector<double> constants = {3.14159265359, 2.54, 1, 6.62606957E-34, }; // a use

template<typename C>
void sort (Cont& c) { /* … */ }

// a generic function

sort(constants);

// a use
Stroustrup - Essence - Going Native'13

14
Not C++ (fundamental)
• No crucial dependence on a garbage collector
– GC is a last and imperfect resort

• No guaranteed type safety
– Not for all constructs
– C compatibility, history, pointers/arrays, unions, casts, …

• No virtual machine
– For many reasons, we often want to run on the real machine
– You can run on a virtual machine (or in a sandbox) if you want to

Stroustrup - Essence - Going Native'13

15
Not C++ (market realities)
• No huge “standard” library
– No owner
• To produce “free” libraries to ensure market share

– No central authority
• To approve, reject, and help integration of libraries

• No standard
– Graphics/GUI
• Competing frameworks

– XML support
– Web support
– …

Stroustrup - Essence - Going Native'13

16
Resource Management

Stroustrup - Essence - Going Native'13

17
Resource management
• A resource should be owned by a “handle”
– A “handle” should present a well-defined and useful abstraction
• E.g. a vector, string, file, thread

• Use constructors and a destructor
class Vector {
// vector of doubles
Vector(initializer_list<double>); // acquire memory; initialize elements
~Vector();
// destroy elements; release memory
// …
private:
double* elem;
// pointer to elements
int sz;
// number of elements
handle
};
void fct()
{
Vector v {1, 1.618, 3.14, 2.99e8};
// …
}

Value
// vector of doubles

Stroustrup - Essence - Going Native'13

18
Resource management
• A handle usually is scoped
– Handles lifetime (initialization, cleanup), and more
Vector::Vector(initializer_list<double> lst)
:elem {new double[lst.size()]}, sz{lst.size()};
{
uninitialized_copy(lst.begin(),lst.end(),elem);
}
Vector::~Vector()
{
delete[] elem;
};

// acquire memory

// initialize elements

// destroy elements; release memory

Stroustrup - Essence - Going Native'13

19
Resource management
• What about errors?
–
–
–
–

A resource is something you acquire and release
A resource should have an owner
Ultimately “root” a resource in a (scoped) handle
“Resource Acquisition Is Initialization” (RAII)
• Acquire during construction
• Release in destructor

– Throw exception in case of failure
• Can be simulated, but not conveniently

– Never throw while holding a resource not owned by a handle

• In general
– Leave established invariants intact when leaving a scope
Stroustrup - Essence - Going Native'13

20
“Resource Acquisition is Initialization” (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 mtx;
int sh;

// a resource
// shared data

void f()
{
std::lock_guard lck {mtx}; // grab (acquire) the mutex
sh+=1;
// manipulate shared data
}
// implicitly release the mutex
Stroustrup - Essence - Going Native'13

21
Pointer Misuse
• 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::runtime_error{“Weird!”};
// leak
if (x<200) return;
// leak
// …
delete p;
// and I want my garbage collector! 
}

– But, garbage collection would not release non-memory resources anyway
– But, why use a “naked” pointer?
Stroustrup - Essence - Going Native'13

22
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::runtime_error{“Weird!”};
// no leak
if (x<200) return;
// no leak
// …
}

– shared_ptr provides a form of garbage collection
– But I’m not sharing anything
•

use a unique_ptr
Stroustrup - Essence - Going Native'13

23
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::runtime_error{“Weird!”};
if (x<200) return;
// …
}

Stroustrup - Essence - Going Native'13

// no leak
// no leak

24
Why do we use pointers?
• And references, iterators, etc.
• To represent ownership
– Don’t! Instead, use handles

• To reference resources
– from within a handle

• To represent positions
– Be careful

• To pass large amounts of data (into a function)
– E.g. pass by const reference

• To return large amount of data (out of a function)
– Don’t! Instead use move operations
Stroustrup - Essence - Going Native'13

25
How to get a lot of data cheaply out of a function?
• Ideas
– Return a pointer to a new’d object
• Who does the delete?

- Return a reference to a new’d object
- Who does the delete?
- Delete what?

- Pass a target object
- We are regressing towards assembly code

- Return an object
- Copies are expensive
- Tricks to avoid copying are brittle
- Tricks to avoid copying are not general

- Return a handle
- Simple and cheap

Stroustrup - Essence - Going Native'13

26
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”
r:
res:
……..
Stroustrup - Essence - Going Native'13

27
Move semantics
• Direct support in C++11: Move constructor
class Matrix {
Representation rep;
// …
Matrix(Matrix&& a)
{
rep = a.rep;
a.rep = {};
}
};

// move constructor
// *this gets a’s elements
// a becomes the empty Matrix

Matrix res = a+b;

r:

res:
……..
Stroustrup - Essence - Going Native'13

28
No garbage collection needed
• For general, simple, implicit, and efficient resource management
• Apply these techniques in order:
1.

Store data in containers
•
•

2.

Manage all resources with resource handles
•
•

3.

RAII
Not just memory: all resources

Use “smart pointers”
•

4.

The semantics of the fundamental abstraction is reflected in the interface
Including lifetime

They are still pointers

Plug in a garbage collector
• For “litter collection”
• C++11 specifies an interface
• Can still leak non-memory- Essence - Going Native'13
resources
Stroustrup

29
Range-for, auto, and move
• As ever, what matters is how features work in combination
template<typename C, typename V>
vector<Value_type<C>*> find_all(C& c, V v) // find all occurrences of v in c
{
vector<Value_type<C>*> res;
for (auto& x : c)
if (x==v)
res.push_back(&x);
return res;
}
string m {"Mary had a little lamb"};
for (const auto p : find_all(m,'a')) // p is a char*
if (*p!='a')
cerr << "string bug!n";

Stroustrup - Essence - Going Native'13

30
RAII and Move Semantics
• All the standard-library containers provide it
•
•
•
•
•
•

vector
list, forward_list (singly-linked list), …
map, unordered_map (hash table),…
set, multi_set, …
…
string

• So do other standard resources
•
•
•
•

thread, lock_guard, …
istream, fstream, …
unique_ptr, shared_ptr
…

Stroustrup - Essence - Going Native'13

31
OOP

Stroustrup - Essence - Going Native'13

32
Class hierarchies

Class’ own members

Derived classes
All users

• Protection model

public

protected

• No universal base class

private

– an unnecessary implementation-oriented artifact
– imposes avoidable space and time overheads.
– encourages underspecified (overly general) interfaces

• Multiple inheritance
– Separately consider interface and implementation
– Abstract classes provide the most stable interfaces

• Minimal run-time type identification
– dynamic_cast<D*>(pb)
Stroustrup - Essence - Going Native'13
– typeid(p)

33
Inheritance
• 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”

Stroustrup - Essence - Going Native'13

34
GP

Stroustrup - Essence - Going Native'13

35
Generic Programming: Templates
• 1980: Use macros to express generic types and functions
• 1987 (and current) aims:
– Extremely general/flexible
• “must be able to do much more than I can imagine”

– Zero-overhead
• vector/Matrix/… to compete with C arrays

– Well-specified interfaces
• Implying overloading, good error messages, and maybe separate
compilation

• “two out of three ain’t bad”
– But it isn’t really good either
– it has kept me concerned/working for 20+ years
Stroustrup - Essence - Going Native'13

36
Templates
• Compile-time duck typing
– Leading to template metaprogramming

• A massive success in C++98, better in C++11, better still in C++14
– STL containers
• template<typename T> class vector { /* … */ };

– STL algorithms
• sort(v.begin(),v.end());

– And much more

• Better support for compile-time programming
– C++11: constexpr (improved in C++14)

Stroustrup - Essence - Going Native'13

37
Algorithms
• Messy code is a major source of errors and inefficiencies
• We must use more explicit, well-designed, and tested algorithms
• The C++ standard-library algorithms are expressed in terms of
half-open sequences [first:last)
– For generality and efficiency
void f(vector<int>& v, list<string>& lst)
{
sort(v.begin(),v.end());

// sort the vector using <

auto p = find(lst.begin(),lst.end(),"Aarhus"); // find “Aarhus” in the list
// …
}

• We parameterize over element type and container type
Stroustrup - Essence - Going Native'13

38
Algorithms
• Simple, efficient, and general implementation
– For any forward iterator
– For any (matching) value type
template<typename Iter, typename Value>
Iter find(Iter first, Iter last, Value val) // find first p in [first:last) so that *p==val
{
while (first!=last && *first!=val)
++first;
return first;
}

Stroustrup - Essence - Going Native'13

39
Algorithms and Function Objects
• Parameterization with criteria, actions, and algorithms
– Essential for flexibility and performance
void g(vector< string>& vs)
{
auto p = find_if(vs.begin(), vs.end(), Less_than{"Griffin"});
// …
}

Stroustrup - Essence - Going Native'13

40
Algorithms and Function Objects
• The implementation is still trivial

template<typename Iter, typename Predicate>
Iter find_if(Iter first, Iter last, Predicate pred) // find first p in [first:last) so that pred(*p)
{
while (first!=last && !pred(*first))
++first;
return first;
}

Stroustrup - Essence - Going Native'13

41
Function Objects and Lambdas
• General function object
– Can carry state
– Easily inlined (i.e., close to optimally efficient)
struct Less_than {
String s;
Less_than(const string& ss) :s{ss} {} // store the value to compare against
bool operator()(const string& v) const { return v<s; } // the comparison
};

Lambda notation
– We can let the compiler write the function object for us
auto p = std::find_if(vs.begin(),vs.end(),
[](const string& v) { return v<"Griffin"; } );
Stroustrup - Essence - Going Native'13

42
Container algorithms
• The C++ standard-library algorithms are expressed in terms of halfopen sequences [first:last)
– For generality and efficiency
– If you find that verbose, define container algorithms

namespace Extended_STL {
// …
template<typename C, typename Predicate>
Iterator<C> find_if(C& c, Predicate pred)
{
return std::find_if(c.begin(),c.end(),pred);
}
// …
}
auto p = find_if(v, [](int x) { return x%2; } );
Stroustrup - Essence - Going Native'13

// assuming v is a vector<int>
43
Duck Typing is Insufficient
• There are no proper interfaces
• Leaves error detection far too late
– Compile- and link-time in C++

• Encourages a focus on implementation details
– Entangles users with implementation

• Leads to over-general interfaces and data structures
– As programmers rely on exposed implementation “details”

• Does not integrate well with other parts of the language
– Teaching and maintenance problems

• We must think of generic code in ways similar to other code
– Relying on well-specified interfaces (like OO, etc.)
Stroustrup - Essence - Going Native'13

44
Generic Programming is just Programming
• Traditional code
double sqrt(double d);
double d = 7;
double d2 = sqrt(d);
double d3 = sqrt(&d);

// C++84: accept any d that is a double
// fine: d is a double
// error: &d is not a double

• Generic code
void sort(Container& c); // C++14: accept any c that is a Container
vector<string> vs { "Hello", "new", "World" };
sort(vs);
// fine: vs is a Container
sort(&vs);
// error: &vs is not a Container

Stroustrup - Essence - Going Native'13

45
C++14: Constraints aka “Concepts lite”
• How do we specify requirements on template arguments?
– state intent
• Explicitly states requirements on argument types

– provide point-of-use checking
• No checking of template definitions

– use constexpr functions

•
•
•
•

Voted as C++14 Technical Report
Design by B. Stroustrup, G. Dos Reis, and A. Sutton
Implemented by Andrew Sutton in GCC
There are no C++0x concept complexities
– No concept maps
– No new syntax for defining concepts
– No new scope and lookup issues - Going Native'13
Stroustrup - Essence

46
What is a Concept?
• Concepts are fundamental
– They represent fundamental concepts of an application area
– Concepts are come in “clusters” describing an application area

• A concept has semantics (meaning)
– Not just syntax
– “Subtractable” is not a concept

• We have always had concepts
–
–
–
–

C++: Integral, arithmetic
STL: forward iterator, predicate
Informally: Container, Sequence
Algebra: Group, Ring, …

Stroustrup - Essence - Going Native'13

47
What is a Concept?
• Don’t expect to find a new fundamental concept every year
• A concept is not the minimal requirements for an implementation
– An implementation does not define the requirements
– Requirements should be stable

• Concepts support interoperability
– There are relatively few concepts
– We can remember a concept

Stroustrup - Essence - Going Native'13

48
C++14 Concepts (Constraints)
• A concept is a predicate on one or more arguments
– E.g. Sequence<T>()

// is T a Sequence?

• Template declaration
template <typename S, typename T>
requires Sequence<S>()
&& Equality_comparable<Value_type<S>, T>()
Iterator_of<S> find(S& seq, const T& value);

• Template use
void use(vector<string>& vs)
{
auto p = find(vs,"Jabberwocky");
// …
Stroustrup - Essence - Going Native'13
}

49
C++14 Concepts: Error handling
• Error handling is simple (and fast)
template<Sortable Cont>
void sort(Cont& container);
vector<double> vec {1.2, 4.5, 0.5, -1.2};
list<int> lst {1, 3, 5, 4, 6, 8,2};
sort(vec);
sort(lst);

// OK: a vector is Sortable
// Error at (this) point of use: Sortable requires random access

• Actual error message
error: ‘list<int>’ does not satisfy the constraint ‘Sortable’

Stroustrup - Essence - Going Native'13

50
C++14 Concepts: “Shorthand Notation”
• Shorthand notation
template <Sequence S, Equality_comparable<Value_type<S>> T>
Iterator_of<C> find(S& seq, const T& value);

• We can handle essentially all of the Palo Alto TR
– (STL algorithms) and more
• Except for the axiom parts

– We see no problems checking template definitions in isolation
• But proposing that would be premature (needs work, experience)

– We don’t need explicit requires much (the shorthand is usually fine)

Stroustrup - Essence - Going Native'13

51
C++14 Concepts: Overloading
• Overloading is easy
template <Sequence S, Equality_comparable<Value_type<S>> T>
Iterator_of<S> find(S& seq, const T& value);
template<Associative_container C>
Iterator_type<C> find(C& assoc, const Key_type<C>& key);
vector<int> v { /* ... */ };
multiset<int> s { /* … */ };
auto vi = find(v, 42);

auto si = find(s, 12-12-12);

// calls 1st overload:
// a vector is a Sequence
// calls 2nd overload:
// a multiset is an Associative_container

Stroustrup - Essence - Going Native'13

52
C++14 Concepts: Overloading
• Overloading based on predicates
– specialization based on subset
– Far easier than writing lots of tests
template<Input_iterator Iter>
void advance(Iter& p, Difference_type<Iter> n) { while (n--) ++p; }
template<Bidirectional_iterator Iter>
void advance(Iter& i, Difference_type<Iter> n)
{ if (n > 0) while (n--) ++p; if (n < 0) while (n++) --ip}
template<Random_access_iterator Iter>
void advance(Iter& p, Difference_type<Iter> n) { p += n; }

• We don’t say
Input_iterator < Bidirectional_iterator < Random_access_iterator

we compute it
Stroustrup - Essence - Going Native'13

53
C++14 Concepts: Definition
• How do you write constraints?
– Any bool expression
• Including type traits and constexpr function

– a requires(expr) expression
• requires() is a compile time intrinsic function
• true if expr is a valid expression

• To recognize a concept syntactically, we can declare it concept
– Rather than just constexpr

Stroustrup - Essence - Going Native'13

54
C++14 Concepts: “Terse Notation”
• We can use a concept name as the name of a type than satisfy
the concept
void sort(Container& c);

// terse notation

– means
template<Container __Cont>
void sort(__Cont& c);

// shorthand notation

– means
template<typename __Cont>
// explicit use of predicate
requires Container<__Cont>()
void sort(__Cont)& c;

– Accepts any type that is a Container
vector<string> vs;
sort(vs);
Stroustrup - Essence - Going Native'13

55
C++14 Concepts: “Terse Notation”
• We have reached the conventional notation
– with the conventional meaning

• Traditional code
double sqrt(double d);
double d = 7;
double d2 = sqrt(d);
double d3 = sqrt(&d);

// C++84: accept any d that is a double
// fine: d is a double
// error: &d is not a double

• Generic code
void sort(Container& c); // C++14: accept any c that is a Container
vector<string> vs { "Hello", "new", "World" };
sort(vs);
// fine: vs is a Container
sort(&vs);
// error: &vs is not a Container
Stroustrup - Essence - Going Native'13

56
C++14 Concepts: “Terse Notation”
• Consider std::merge
• Explicit use of predicates:
template<typename For,
typename For2,
typename Out>
requires Forward_iterator<For>()
&& Forward_iterator<For2>()
&& Output_iterator<Out>()
&& Assignable<Value_type<For>,Value_type<Out>>()
&& Assignable<Value_type<For2,Value_type<Out>>()
&& Comparable<Value_type<For>,Value_type<For2>>()
void merge(For p, For q, For2 p2, For2 q2, Out p);

• Headache inducing, and accumulate() is worse
Stroustrup - Essence - Going Native'13

57
C++14 Concepts: “Terse Notation”
• Better, use the shorthand notation
template<Forward_iterator For,
Forward_iterator For2,
Output_iterator Out>
requires Mergeable<For,For2,Out>()
void merge(For p, For q, For2 p2, For2 q2, Out p);

• Quite readable

Stroustrup - Essence - Going Native'13

58
C++14 Concepts: “Terse Notation”
• Better still, use the “terse notation”:
Mergeable{For,For2,Out} // Mergeable is a concept requiring three types
void merge(For p, For q, For2 p2, For2 q2, Out p);

• The
concept-name { identifier-list }

notation introduces constrained names

• Make simple things simple!
Stroustrup - Essence - Going Native'13

59
C++14 Concepts: “Terse Notation”
• Now we just need to define Mergeable:
template<typename For, typename For2, typename Out>
concept bool Mergeable()
{
return Forward_iterator<For>()
&& Forward_iterator<For2>()
&& Output_iterator<Out>()
&& Assignable<Value_type<For>,Value_type<Out>>()
&& Assignable<Value_type<For2,Value_type<Out>>()
&& Comparable<Value_type<For>,Value_type<For2>>();
}

• It’s just a predicate

Stroustrup - Essence - Going Native'13

60
Challenges

Stroustrup - Essence - Going Native'13

61
C++ Challenges
• Obviously, C++ is not perfect
– How can we make programmers prefer modern styles over low-level code
• which is far more error-prone and harder to maintain, yet no more efficient?

– How can we make C++ a better language given the Draconian constraints
of C and C++ compatibility?
– How can we improve and complete the techniques and models
(incompletely and imperfectly) embodied in C++?

• Solutions that eliminate major C++ strengths are not acceptable
– Compatibility
• link, source code

– Performance
• uncompromising

– Portability
– Range of application areas
• Preferably increasing the range
Stroustrup - Essence - Going Native'13

62
Long-term C++ Challenges
• Close more type loopholes
– in particular, find a way to prevent misuses of delete without spoiling RAII

• Simplify concurrent programming
– in particular, provide some higher-level concurrency models as libraries

• Simplify generic programming
– in particular, introduce simple and effective concepts

• Simplify programming using class hierarchies
– in particular, eliminate use of the visitor pattern

• Better support for combinations of object-oriented and generic programming
• Make exceptions usable for hard-real-time projects
– that will most likely be a tool rather than a language change

• Find a good way of using multiple address spaces
– as needed for distributed computing
– would probably involve defining a more general module mechanism that would
also address dynamic linking, and more.

• Provide many more domain-specific libraries
• Develop a more precise and formal specification of C++
Stroustrup - Essence - Going Native'13

63
“Paradigms”
• Much of the distinction between object-oriented
programming, generic programming, and “conventional
programming” is an illusion
– based on a focus on language features
– incomplete support for a synthesis of techniques
– The distinction does harm
• by limiting programmers, forcing workarounds

void draw_all(Container& c) // is this OOP, GP, or conventional?
requires Same_type<Value_type<Container>,Shape*>
{
for_each(c, [](Shape* p) { p->draw(); } );
}
Stroustrup - Essence - Going Native'13

64
Questions?
C++: A light-weight abstraction
programming language

Key strengths:
• software infrastructure
• resource-constrained applications
Stroustrup - Essence - Going Native'13

Practice type-rich
programming
65

Contenu connexe

Tendances

Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++Mihai Todor
 
C++ Generators and Property-based Testing
C++ Generators and Property-based TestingC++ Generators and Property-based Testing
C++ Generators and Property-based TestingSumant Tambe
 
Design Patterns in Modern C++
Design Patterns in Modern C++Design Patterns in Modern C++
Design Patterns in Modern C++Dmitri Nesteruk
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMSfawzmasood
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanJimin Hsieh
 
Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Kel Cecil
 
Tensor flow (1)
Tensor flow (1)Tensor flow (1)
Tensor flow (1)景逸 王
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Alessio Tonioni
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumlercorehard_by
 
Communicating State Machines
Communicating State MachinesCommunicating State Machines
Communicating State Machinessrirammalhar
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting StartedHadziq Fabroyir
 
TensorFlow example for AI Ukraine2016
TensorFlow example  for AI Ukraine2016TensorFlow example  for AI Ukraine2016
TensorFlow example for AI Ukraine2016Andrii Babii
 
Intro to Python (High School) Unit #2
Intro to Python (High School) Unit #2Intro to Python (High School) Unit #2
Intro to Python (High School) Unit #2Jay Coskey
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and MapsIntro C# Book
 
The Rust Programming Language: an Overview
The Rust Programming Language: an OverviewThe Rust Programming Language: an Overview
The Rust Programming Language: an OverviewRoberto Casadei
 

Tendances (20)

Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
C++ Generators and Property-based Testing
C++ Generators and Property-based TestingC++ Generators and Property-based Testing
C++ Generators and Property-based Testing
 
Design Patterns in Modern C++
Design Patterns in Modern C++Design Patterns in Modern C++
Design Patterns in Modern C++
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
 
Monte Carlo C++
Monte Carlo C++Monte Carlo C++
Monte Carlo C++
 
Tensor flow (1)
Tensor flow (1)Tensor flow (1)
Tensor flow (1)
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Tensorflow - Intro (2017)
Tensorflow - Intro (2017)
 
Why rust?
Why rust?Why rust?
Why rust?
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
Communicating State Machines
Communicating State MachinesCommunicating State Machines
Communicating State Machines
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started
 
TensorFlow example for AI Ukraine2016
TensorFlow example  for AI Ukraine2016TensorFlow example  for AI Ukraine2016
TensorFlow example for AI Ukraine2016
 
Intro to Python (High School) Unit #2
Intro to Python (High School) Unit #2Intro to Python (High School) Unit #2
Intro to Python (High School) Unit #2
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
The Rust Programming Language: an Overview
The Rust Programming Language: an OverviewThe Rust Programming Language: an Overview
The Rust Programming Language: an Overview
 

En vedette

Startup Idea Formation and Creation
Startup Idea Formation and CreationStartup Idea Formation and Creation
Startup Idea Formation and CreationLaunch Angels
 
Es consulting webinar series from idea to innovation mike allen of everything...
Es consulting webinar series from idea to innovation mike allen of everything...Es consulting webinar series from idea to innovation mike allen of everything...
Es consulting webinar series from idea to innovation mike allen of everything...Michael Allen
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing SoftwareSteven Smith
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingfarhan amjad
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo designConfiz
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++ shammi mehra
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templatesfarhan amjad
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
Building Embedded Linux
Building Embedded LinuxBuilding Embedded Linux
Building Embedded LinuxSherif Mousa
 
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonThrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonIgor Anishchenko
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Liju Thomas
 

En vedette (20)

Startup Idea Formation and Creation
Startup Idea Formation and CreationStartup Idea Formation and Creation
Startup Idea Formation and Creation
 
Es consulting webinar series from idea to innovation mike allen of everything...
Es consulting webinar series from idea to innovation mike allen of everything...Es consulting webinar series from idea to innovation mike allen of everything...
Es consulting webinar series from idea to innovation mike allen of everything...
 
Idiomatic C++
Idiomatic C++Idiomatic C++
Idiomatic C++
 
Distributed Systems Design
Distributed Systems DesignDistributed Systems Design
Distributed Systems Design
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Web Service Basics and NWS Setup
Web Service  Basics and NWS SetupWeb Service  Basics and NWS Setup
Web Service Basics and NWS Setup
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
SOLID Principles part 2
SOLID Principles part 2SOLID Principles part 2
SOLID Principles part 2
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
SOLID Principles part 1
SOLID Principles part 1SOLID Principles part 1
SOLID Principles part 1
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Building Embedded Linux
Building Embedded LinuxBuilding Embedded Linux
Building Embedded Linux
 
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased ComparisonThrift vs Protocol Buffers vs Avro - Biased Comparison
Thrift vs Protocol Buffers vs Avro - Biased Comparison
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 

Similaire à Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11, and C++14

UsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptUsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptvinu28455
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.pptDevliNeeraj
 
Apache Arrow (Strata-Hadoop World San Jose 2016)
Apache Arrow (Strata-Hadoop World San Jose 2016)Apache Arrow (Strata-Hadoop World San Jose 2016)
Apache Arrow (Strata-Hadoop World San Jose 2016)Wes McKinney
 
Trends and future of C++: Evolving a systems language for performance - by Bj...
Trends and future of C++: Evolving a systems language for performance - by Bj...Trends and future of C++: Evolving a systems language for performance - by Bj...
Trends and future of C++: Evolving a systems language for performance - by Bj...devstonez
 
Rust "Hot or Not" at Sioux
Rust "Hot or Not" at SiouxRust "Hot or Not" at Sioux
Rust "Hot or Not" at Siouxnikomatsakis
 
C++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of ClassC++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of ClassYogendra Rampuria
 
designpatterns_blair_upe.ppt
designpatterns_blair_upe.pptdesignpatterns_blair_upe.ppt
designpatterns_blair_upe.pptbanti43
 
Open Source Systems Performance
Open Source Systems PerformanceOpen Source Systems Performance
Open Source Systems PerformanceBrendan Gregg
 
Beware of Pointers
Beware of PointersBeware of Pointers
Beware of Pointersppd1961
 

Similaire à Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11, and C++14 (20)

Bjarne essencegn13
Bjarne essencegn13Bjarne essencegn13
Bjarne essencegn13
 
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
 
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
 
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
 
Apache Arrow (Strata-Hadoop World San Jose 2016)
Apache Arrow (Strata-Hadoop World San Jose 2016)Apache Arrow (Strata-Hadoop World San Jose 2016)
Apache Arrow (Strata-Hadoop World San Jose 2016)
 
Trends and future of C++: Evolving a systems language for performance - by Bj...
Trends and future of C++: Evolving a systems language for performance - by Bj...Trends and future of C++: Evolving a systems language for performance - by Bj...
Trends and future of C++: Evolving a systems language for performance - by Bj...
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Rust "Hot or Not" at Sioux
Rust "Hot or Not" at SiouxRust "Hot or Not" at Sioux
Rust "Hot or Not" at Sioux
 
C++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of ClassC++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of Class
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
 
designpatterns_blair_upe.ppt
designpatterns_blair_upe.pptdesignpatterns_blair_upe.ppt
designpatterns_blair_upe.ppt
 
Open Source Systems Performance
Open Source Systems PerformanceOpen Source Systems Performance
Open Source Systems Performance
 
Return of c++
Return of c++Return of c++
Return of c++
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
Beware of Pointers
Beware of PointersBeware of Pointers
Beware of Pointers
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 

Dernier (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11, and C++14

  • 1. The Essence of C++ with examples in C++84, C++98, C++11, and C++14 Bjarne Stroustrup Texas A&M University www.stroustrup.com
  • 2. Overview • • • • Aims and constraints C++ in four slides Resource management OOP: Classes and Hierarchies – (very briefly) • GP: Templates – Requirements checking • Challenges Stroustrup - Essence - Going Native'13 3
  • 3. What did/do I want? • Type safety – Encapsulate necessary unsafe operations • Resource safety – It’s not all memory • Performance – For some parts of almost all systems, it’s important • Predictability – For hard and soft real time • Teachability – Complexity of code should be proportional to the complexity of the task • Readability – People and machines (“analyzability”) Stroustrup - Essence - Going Native'13 4
  • 4. Who did/do I want it for? • Primary concerns – – – – Systems programming Embedded systems Resource constrained systems Large systems • Experts – “C++ is expert friendly” • Novices – C++ Is not just expert friendly Stroustrup - Essence - Going Native'13 5
  • 5. Template meta-programming! What is C++? Class hierarchies A hybrid language A multi-paradigm programming language Buffer overflows It’s C! Classes Embedded systems programming language Too big! An object-oriented programming language Generic programming Stroustrup - Essence - Going Native'13 Low level! A random collection of features 6
  • 6. C++ A light-weight abstraction programming language Key strengths: • software infrastructure • resource-constrained applications Stroustrup - Essence - Going Native'13 7
  • 7. Programming Languages Domain-specific abstraction General-purpose abstraction Fortran Cobol Simula Java C++ C++11 Direct mapping to hardware Assembler BCPL C Stroustrup - Essence - Going Native'13 C# 8
  • 8. What does C++ offer? • Not perfection – Of course • Not everything for everybody – Of course • A solid fundamental model – Yes, really • 30+ years of real-world “refinement” – It works • Performance – A match for anything • The best is buried in “compatibility stuff’’ – long-term stability is a feature Stroustrup - Essence - Going Native'13 9
  • 9. What does C++ offer? • C++ in Four slides – – – – Map to hardware Classes Inheritance Parameterized types • If you understand int and vector, you understand C++ – The rest is “details” (1,300+ pages of details) Stroustrup - Essence - Going Native'13 10
  • 10. Map to Hardware • Primitive operations => instructions – +, %, ->, [], (), … value • int, double, complex<double>, Date, … handle • vector, string, thread, Matrix, … value • Objects can be composed by simple concatenation: – Arrays – Classes/structs value handle value handle value value Stroustrup - Essence - Going Native'13 11
  • 11. Classes: Construction/Destruction • From the first week of “C with Classes” (1979) class X { // user-defined type public: // interface X(Something); // constructor from Something ~X(); // destructor // … private: // implementation // … }; “A constructor establishes the environment for the members to run in; the destructor reverses its actions.” Stroustrup - Essence - Going Native'13 12
  • 12. Abstract Classes and Inheritance • Insulate the user from the implementation struct Device { virtual int put(const char*) = 0; virtual int get(const char*) = 0; }; // abstract class // pure virtual function • No data members, all data in derived classes – “not brittle” • Manipulate through pointer or reference – Typically allocated on the free store (“dynamic memory”) – Typically requires some form of lifetime management (use resource handles) • Is the root of a hierarchy of derived classes Stroustrup - Essence - Going Native'13 13
  • 13. Parameterized Types and Classes • Templates – Essential: Support for generic programming – Secondary: Support for compile-time computation template<typename T> class vector { /* … */ }; // a generic type vector<double> constants = {3.14159265359, 2.54, 1, 6.62606957E-34, }; // a use template<typename C> void sort (Cont& c) { /* … */ } // a generic function sort(constants); // a use Stroustrup - Essence - Going Native'13 14
  • 14. Not C++ (fundamental) • No crucial dependence on a garbage collector – GC is a last and imperfect resort • No guaranteed type safety – Not for all constructs – C compatibility, history, pointers/arrays, unions, casts, … • No virtual machine – For many reasons, we often want to run on the real machine – You can run on a virtual machine (or in a sandbox) if you want to Stroustrup - Essence - Going Native'13 15
  • 15. Not C++ (market realities) • No huge “standard” library – No owner • To produce “free” libraries to ensure market share – No central authority • To approve, reject, and help integration of libraries • No standard – Graphics/GUI • Competing frameworks – XML support – Web support – … Stroustrup - Essence - Going Native'13 16
  • 16. Resource Management Stroustrup - Essence - Going Native'13 17
  • 17. Resource management • A resource should be owned by a “handle” – A “handle” should present a well-defined and useful abstraction • E.g. a vector, string, file, thread • Use constructors and a destructor class Vector { // vector of doubles Vector(initializer_list<double>); // acquire memory; initialize elements ~Vector(); // destroy elements; release memory // … private: double* elem; // pointer to elements int sz; // number of elements handle }; void fct() { Vector v {1, 1.618, 3.14, 2.99e8}; // … } Value // vector of doubles Stroustrup - Essence - Going Native'13 18
  • 18. Resource management • A handle usually is scoped – Handles lifetime (initialization, cleanup), and more Vector::Vector(initializer_list<double> lst) :elem {new double[lst.size()]}, sz{lst.size()}; { uninitialized_copy(lst.begin(),lst.end(),elem); } Vector::~Vector() { delete[] elem; }; // acquire memory // initialize elements // destroy elements; release memory Stroustrup - Essence - Going Native'13 19
  • 19. Resource management • What about errors? – – – – A resource is something you acquire and release A resource should have an owner Ultimately “root” a resource in a (scoped) handle “Resource Acquisition Is Initialization” (RAII) • Acquire during construction • Release in destructor – Throw exception in case of failure • Can be simulated, but not conveniently – Never throw while holding a resource not owned by a handle • In general – Leave established invariants intact when leaving a scope Stroustrup - Essence - Going Native'13 20
  • 20. “Resource Acquisition is Initialization” (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 mtx; int sh; // a resource // shared data void f() { std::lock_guard lck {mtx}; // grab (acquire) the mutex sh+=1; // manipulate shared data } // implicitly release the mutex Stroustrup - Essence - Going Native'13 21
  • 21. Pointer Misuse • 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::runtime_error{“Weird!”}; // leak if (x<200) return; // leak // … delete p; // and I want my garbage collector!  } – But, garbage collection would not release non-memory resources anyway – But, why use a “naked” pointer? Stroustrup - Essence - Going Native'13 22
  • 22. 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::runtime_error{“Weird!”}; // no leak if (x<200) return; // no leak // … } – shared_ptr provides a form of garbage collection – But I’m not sharing anything • use a unique_ptr Stroustrup - Essence - Going Native'13 23
  • 23. 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::runtime_error{“Weird!”}; if (x<200) return; // … } Stroustrup - Essence - Going Native'13 // no leak // no leak 24
  • 24. Why do we use pointers? • And references, iterators, etc. • To represent ownership – Don’t! Instead, use handles • To reference resources – from within a handle • To represent positions – Be careful • To pass large amounts of data (into a function) – E.g. pass by const reference • To return large amount of data (out of a function) – Don’t! Instead use move operations Stroustrup - Essence - Going Native'13 25
  • 25. How to get a lot of data cheaply out of a function? • Ideas – Return a pointer to a new’d object • Who does the delete? - Return a reference to a new’d object - Who does the delete? - Delete what? - Pass a target object - We are regressing towards assembly code - Return an object - Copies are expensive - Tricks to avoid copying are brittle - Tricks to avoid copying are not general - Return a handle - Simple and cheap Stroustrup - Essence - Going Native'13 26
  • 26. 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” r: res: …….. Stroustrup - Essence - Going Native'13 27
  • 27. Move semantics • Direct support in C++11: Move constructor class Matrix { Representation rep; // … Matrix(Matrix&& a) { rep = a.rep; a.rep = {}; } }; // move constructor // *this gets a’s elements // a becomes the empty Matrix Matrix res = a+b; r: res: …….. Stroustrup - Essence - Going Native'13 28
  • 28. No garbage collection needed • For general, simple, implicit, and efficient resource management • Apply these techniques in order: 1. Store data in containers • • 2. Manage all resources with resource handles • • 3. RAII Not just memory: all resources Use “smart pointers” • 4. The semantics of the fundamental abstraction is reflected in the interface Including lifetime They are still pointers Plug in a garbage collector • For “litter collection” • C++11 specifies an interface • Can still leak non-memory- Essence - Going Native'13 resources Stroustrup 29
  • 29. Range-for, auto, and move • As ever, what matters is how features work in combination template<typename C, typename V> vector<Value_type<C>*> find_all(C& c, V v) // find all occurrences of v in c { vector<Value_type<C>*> res; for (auto& x : c) if (x==v) res.push_back(&x); return res; } string m {"Mary had a little lamb"}; for (const auto p : find_all(m,'a')) // p is a char* if (*p!='a') cerr << "string bug!n"; Stroustrup - Essence - Going Native'13 30
  • 30. RAII and Move Semantics • All the standard-library containers provide it • • • • • • vector list, forward_list (singly-linked list), … map, unordered_map (hash table),… set, multi_set, … … string • So do other standard resources • • • • thread, lock_guard, … istream, fstream, … unique_ptr, shared_ptr … Stroustrup - Essence - Going Native'13 31
  • 31. OOP Stroustrup - Essence - Going Native'13 32
  • 32. Class hierarchies Class’ own members Derived classes All users • Protection model public protected • No universal base class private – an unnecessary implementation-oriented artifact – imposes avoidable space and time overheads. – encourages underspecified (overly general) interfaces • Multiple inheritance – Separately consider interface and implementation – Abstract classes provide the most stable interfaces • Minimal run-time type identification – dynamic_cast<D*>(pb) Stroustrup - Essence - Going Native'13 – typeid(p) 33
  • 33. Inheritance • 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” Stroustrup - Essence - Going Native'13 34
  • 34. GP Stroustrup - Essence - Going Native'13 35
  • 35. Generic Programming: Templates • 1980: Use macros to express generic types and functions • 1987 (and current) aims: – Extremely general/flexible • “must be able to do much more than I can imagine” – Zero-overhead • vector/Matrix/… to compete with C arrays – Well-specified interfaces • Implying overloading, good error messages, and maybe separate compilation • “two out of three ain’t bad” – But it isn’t really good either – it has kept me concerned/working for 20+ years Stroustrup - Essence - Going Native'13 36
  • 36. Templates • Compile-time duck typing – Leading to template metaprogramming • A massive success in C++98, better in C++11, better still in C++14 – STL containers • template<typename T> class vector { /* … */ }; – STL algorithms • sort(v.begin(),v.end()); – And much more • Better support for compile-time programming – C++11: constexpr (improved in C++14) Stroustrup - Essence - Going Native'13 37
  • 37. Algorithms • Messy code is a major source of errors and inefficiencies • We must use more explicit, well-designed, and tested algorithms • The C++ standard-library algorithms are expressed in terms of half-open sequences [first:last) – For generality and efficiency void f(vector<int>& v, list<string>& lst) { sort(v.begin(),v.end()); // sort the vector using < auto p = find(lst.begin(),lst.end(),"Aarhus"); // find “Aarhus” in the list // … } • We parameterize over element type and container type Stroustrup - Essence - Going Native'13 38
  • 38. Algorithms • Simple, efficient, and general implementation – For any forward iterator – For any (matching) value type template<typename Iter, typename Value> Iter find(Iter first, Iter last, Value val) // find first p in [first:last) so that *p==val { while (first!=last && *first!=val) ++first; return first; } Stroustrup - Essence - Going Native'13 39
  • 39. Algorithms and Function Objects • Parameterization with criteria, actions, and algorithms – Essential for flexibility and performance void g(vector< string>& vs) { auto p = find_if(vs.begin(), vs.end(), Less_than{"Griffin"}); // … } Stroustrup - Essence - Going Native'13 40
  • 40. Algorithms and Function Objects • The implementation is still trivial template<typename Iter, typename Predicate> Iter find_if(Iter first, Iter last, Predicate pred) // find first p in [first:last) so that pred(*p) { while (first!=last && !pred(*first)) ++first; return first; } Stroustrup - Essence - Going Native'13 41
  • 41. Function Objects and Lambdas • General function object – Can carry state – Easily inlined (i.e., close to optimally efficient) struct Less_than { String s; Less_than(const string& ss) :s{ss} {} // store the value to compare against bool operator()(const string& v) const { return v<s; } // the comparison }; Lambda notation – We can let the compiler write the function object for us auto p = std::find_if(vs.begin(),vs.end(), [](const string& v) { return v<"Griffin"; } ); Stroustrup - Essence - Going Native'13 42
  • 42. Container algorithms • The C++ standard-library algorithms are expressed in terms of halfopen sequences [first:last) – For generality and efficiency – If you find that verbose, define container algorithms namespace Extended_STL { // … template<typename C, typename Predicate> Iterator<C> find_if(C& c, Predicate pred) { return std::find_if(c.begin(),c.end(),pred); } // … } auto p = find_if(v, [](int x) { return x%2; } ); Stroustrup - Essence - Going Native'13 // assuming v is a vector<int> 43
  • 43. Duck Typing is Insufficient • There are no proper interfaces • Leaves error detection far too late – Compile- and link-time in C++ • Encourages a focus on implementation details – Entangles users with implementation • Leads to over-general interfaces and data structures – As programmers rely on exposed implementation “details” • Does not integrate well with other parts of the language – Teaching and maintenance problems • We must think of generic code in ways similar to other code – Relying on well-specified interfaces (like OO, etc.) Stroustrup - Essence - Going Native'13 44
  • 44. Generic Programming is just Programming • Traditional code double sqrt(double d); double d = 7; double d2 = sqrt(d); double d3 = sqrt(&d); // C++84: accept any d that is a double // fine: d is a double // error: &d is not a double • Generic code void sort(Container& c); // C++14: accept any c that is a Container vector<string> vs { "Hello", "new", "World" }; sort(vs); // fine: vs is a Container sort(&vs); // error: &vs is not a Container Stroustrup - Essence - Going Native'13 45
  • 45. C++14: Constraints aka “Concepts lite” • How do we specify requirements on template arguments? – state intent • Explicitly states requirements on argument types – provide point-of-use checking • No checking of template definitions – use constexpr functions • • • • Voted as C++14 Technical Report Design by B. Stroustrup, G. Dos Reis, and A. Sutton Implemented by Andrew Sutton in GCC There are no C++0x concept complexities – No concept maps – No new syntax for defining concepts – No new scope and lookup issues - Going Native'13 Stroustrup - Essence 46
  • 46. What is a Concept? • Concepts are fundamental – They represent fundamental concepts of an application area – Concepts are come in “clusters” describing an application area • A concept has semantics (meaning) – Not just syntax – “Subtractable” is not a concept • We have always had concepts – – – – C++: Integral, arithmetic STL: forward iterator, predicate Informally: Container, Sequence Algebra: Group, Ring, … Stroustrup - Essence - Going Native'13 47
  • 47. What is a Concept? • Don’t expect to find a new fundamental concept every year • A concept is not the minimal requirements for an implementation – An implementation does not define the requirements – Requirements should be stable • Concepts support interoperability – There are relatively few concepts – We can remember a concept Stroustrup - Essence - Going Native'13 48
  • 48. C++14 Concepts (Constraints) • A concept is a predicate on one or more arguments – E.g. Sequence<T>() // is T a Sequence? • Template declaration template <typename S, typename T> requires Sequence<S>() && Equality_comparable<Value_type<S>, T>() Iterator_of<S> find(S& seq, const T& value); • Template use void use(vector<string>& vs) { auto p = find(vs,"Jabberwocky"); // … Stroustrup - Essence - Going Native'13 } 49
  • 49. C++14 Concepts: Error handling • Error handling is simple (and fast) template<Sortable Cont> void sort(Cont& container); vector<double> vec {1.2, 4.5, 0.5, -1.2}; list<int> lst {1, 3, 5, 4, 6, 8,2}; sort(vec); sort(lst); // OK: a vector is Sortable // Error at (this) point of use: Sortable requires random access • Actual error message error: ‘list<int>’ does not satisfy the constraint ‘Sortable’ Stroustrup - Essence - Going Native'13 50
  • 50. C++14 Concepts: “Shorthand Notation” • Shorthand notation template <Sequence S, Equality_comparable<Value_type<S>> T> Iterator_of<C> find(S& seq, const T& value); • We can handle essentially all of the Palo Alto TR – (STL algorithms) and more • Except for the axiom parts – We see no problems checking template definitions in isolation • But proposing that would be premature (needs work, experience) – We don’t need explicit requires much (the shorthand is usually fine) Stroustrup - Essence - Going Native'13 51
  • 51. C++14 Concepts: Overloading • Overloading is easy template <Sequence S, Equality_comparable<Value_type<S>> T> Iterator_of<S> find(S& seq, const T& value); template<Associative_container C> Iterator_type<C> find(C& assoc, const Key_type<C>& key); vector<int> v { /* ... */ }; multiset<int> s { /* … */ }; auto vi = find(v, 42); auto si = find(s, 12-12-12); // calls 1st overload: // a vector is a Sequence // calls 2nd overload: // a multiset is an Associative_container Stroustrup - Essence - Going Native'13 52
  • 52. C++14 Concepts: Overloading • Overloading based on predicates – specialization based on subset – Far easier than writing lots of tests template<Input_iterator Iter> void advance(Iter& p, Difference_type<Iter> n) { while (n--) ++p; } template<Bidirectional_iterator Iter> void advance(Iter& i, Difference_type<Iter> n) { if (n > 0) while (n--) ++p; if (n < 0) while (n++) --ip} template<Random_access_iterator Iter> void advance(Iter& p, Difference_type<Iter> n) { p += n; } • We don’t say Input_iterator < Bidirectional_iterator < Random_access_iterator we compute it Stroustrup - Essence - Going Native'13 53
  • 53. C++14 Concepts: Definition • How do you write constraints? – Any bool expression • Including type traits and constexpr function – a requires(expr) expression • requires() is a compile time intrinsic function • true if expr is a valid expression • To recognize a concept syntactically, we can declare it concept – Rather than just constexpr Stroustrup - Essence - Going Native'13 54
  • 54. C++14 Concepts: “Terse Notation” • We can use a concept name as the name of a type than satisfy the concept void sort(Container& c); // terse notation – means template<Container __Cont> void sort(__Cont& c); // shorthand notation – means template<typename __Cont> // explicit use of predicate requires Container<__Cont>() void sort(__Cont)& c; – Accepts any type that is a Container vector<string> vs; sort(vs); Stroustrup - Essence - Going Native'13 55
  • 55. C++14 Concepts: “Terse Notation” • We have reached the conventional notation – with the conventional meaning • Traditional code double sqrt(double d); double d = 7; double d2 = sqrt(d); double d3 = sqrt(&d); // C++84: accept any d that is a double // fine: d is a double // error: &d is not a double • Generic code void sort(Container& c); // C++14: accept any c that is a Container vector<string> vs { "Hello", "new", "World" }; sort(vs); // fine: vs is a Container sort(&vs); // error: &vs is not a Container Stroustrup - Essence - Going Native'13 56
  • 56. C++14 Concepts: “Terse Notation” • Consider std::merge • Explicit use of predicates: template<typename For, typename For2, typename Out> requires Forward_iterator<For>() && Forward_iterator<For2>() && Output_iterator<Out>() && Assignable<Value_type<For>,Value_type<Out>>() && Assignable<Value_type<For2,Value_type<Out>>() && Comparable<Value_type<For>,Value_type<For2>>() void merge(For p, For q, For2 p2, For2 q2, Out p); • Headache inducing, and accumulate() is worse Stroustrup - Essence - Going Native'13 57
  • 57. C++14 Concepts: “Terse Notation” • Better, use the shorthand notation template<Forward_iterator For, Forward_iterator For2, Output_iterator Out> requires Mergeable<For,For2,Out>() void merge(For p, For q, For2 p2, For2 q2, Out p); • Quite readable Stroustrup - Essence - Going Native'13 58
  • 58. C++14 Concepts: “Terse Notation” • Better still, use the “terse notation”: Mergeable{For,For2,Out} // Mergeable is a concept requiring three types void merge(For p, For q, For2 p2, For2 q2, Out p); • The concept-name { identifier-list } notation introduces constrained names • Make simple things simple! Stroustrup - Essence - Going Native'13 59
  • 59. C++14 Concepts: “Terse Notation” • Now we just need to define Mergeable: template<typename For, typename For2, typename Out> concept bool Mergeable() { return Forward_iterator<For>() && Forward_iterator<For2>() && Output_iterator<Out>() && Assignable<Value_type<For>,Value_type<Out>>() && Assignable<Value_type<For2,Value_type<Out>>() && Comparable<Value_type<For>,Value_type<For2>>(); } • It’s just a predicate Stroustrup - Essence - Going Native'13 60
  • 60. Challenges Stroustrup - Essence - Going Native'13 61
  • 61. C++ Challenges • Obviously, C++ is not perfect – How can we make programmers prefer modern styles over low-level code • which is far more error-prone and harder to maintain, yet no more efficient? – How can we make C++ a better language given the Draconian constraints of C and C++ compatibility? – How can we improve and complete the techniques and models (incompletely and imperfectly) embodied in C++? • Solutions that eliminate major C++ strengths are not acceptable – Compatibility • link, source code – Performance • uncompromising – Portability – Range of application areas • Preferably increasing the range Stroustrup - Essence - Going Native'13 62
  • 62. Long-term C++ Challenges • Close more type loopholes – in particular, find a way to prevent misuses of delete without spoiling RAII • Simplify concurrent programming – in particular, provide some higher-level concurrency models as libraries • Simplify generic programming – in particular, introduce simple and effective concepts • Simplify programming using class hierarchies – in particular, eliminate use of the visitor pattern • Better support for combinations of object-oriented and generic programming • Make exceptions usable for hard-real-time projects – that will most likely be a tool rather than a language change • Find a good way of using multiple address spaces – as needed for distributed computing – would probably involve defining a more general module mechanism that would also address dynamic linking, and more. • Provide many more domain-specific libraries • Develop a more precise and formal specification of C++ Stroustrup - Essence - Going Native'13 63
  • 63. “Paradigms” • Much of the distinction between object-oriented programming, generic programming, and “conventional programming” is an illusion – based on a focus on language features – incomplete support for a synthesis of techniques – The distinction does harm • by limiting programmers, forcing workarounds void draw_all(Container& c) // is this OOP, GP, or conventional? requires Same_type<Value_type<Container>,Shape*> { for_each(c, [](Shape* p) { p->draw(); } ); } Stroustrup - Essence - Going Native'13 64
  • 64. Questions? C++: A light-weight abstraction programming language Key strengths: • software infrastructure • resource-constrained applications Stroustrup - Essence - Going Native'13 Practice type-rich programming 65