1. C++ by Choice
Florentin Picioroaga
IDS GmbH, Ettlingen, Germany
filo.rom@gmail.com
February 24, 2016
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 1 / 18
2. Overview
1 Motivation
2 Why C++?
Popularity
General features
Static vs. Dynamic type system
Supports many paradigms
3 When C++?
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 2 / 18
3. Motivation
Why C++, when there are so many programming languages?
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 3 / 18
4. Motivation
Why C++, when there are so many programming languages?
When C++? What is the best match (application, programming
language = C++)?
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 3 / 18
5. Motivation
Why C++, when there are so many programming languages?
When C++? What is the best match (application, programming
language = C++)?
Not in the scope:
Why not C++?
When not C++?
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 3 / 18
6. Popularity
Popularity is ,,the fact that something or someone is liked, enjoyed, or supported by
many people” (Cambridge)
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 4 / 18
8. Popularity
Why is C++ popular?
Compatibility with C, no. 1 or 2 in the last 20 years:
source code compatibility
object code compatibility
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 6 / 18
10. General features
General-purpose undo mechanism with destructors
Undo for: memory, mutexes, opening files
RAII, implemented in C++ with constructors/destructors
Guaranteed to be executed even when an exception occurs, only for the
objects on the stack
Deterministic finalization in contrast with GC systems
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 7 / 18
11. General features
General-purpose undo mechanism with destructors
Undo for: memory, mutexes, opening files
RAII, implemented in C++ with constructors/destructors
Guaranteed to be executed even when an exception occurs, only for the
objects on the stack
Deterministic finalization in contrast with GC systems
templates
generic programming (STL)
TMP (Template Meta-Programming)
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 7 / 18
12. General features
General-purpose undo mechanism with destructors
Undo for: memory, mutexes, opening files
RAII, implemented in C++ with constructors/destructors
Guaranteed to be executed even when an exception occurs, only for the
objects on the stack
Deterministic finalization in contrast with GC systems
templates
generic programming (STL)
TMP (Template Meta-Programming)
overloading
function objects, the basis of lambdas in C++11
smart pointers
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 7 / 18
13. Static vs. Dynamic type system
from formal logic we have two properties we can use to describe any
evaluation procedure: soundness and completeness
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 8 / 18
14. Static vs. Dynamic type system
from formal logic we have two properties we can use to describe any
evaluation procedure: soundness and completeness
a sound type system is one that rejects all erroneous programs, and a
complete system accepts all correct programs
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 8 / 18
15. Static vs. Dynamic type system
a programming language with a complete and unsound (static) type
system has a fallback in the form of dynamic typing
dynamically typed means that types are attached to values at run time
statically typed means that types are checked at compile time, and a
program that does not have a static type is rejected by the compiler
combined power of both type systems:
Mozilla Firefox: a core of statically-typed C++, with dynamically-typed
JavaScript running the user interface
many large video games, e.g. World of WarCraft: C++ and Lua
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 9 / 18
16. Static vs. Dynamic type system
is static or dynamic typing more convenient?
auto func = [] (int x) {
if (x >= 0) return 2*x;
return "negative number";
};
std::cout << func(2) << func(-1);
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 10 / 18
17. Static vs. Dynamic type system
is static or dynamic typing more convenient?
auto func = [] (int x) {
if (x >= 0) return 2*x;
return "negative number";
};
std::cout << func(2) << func(-1);
More convenient to write code in a dynamic language but more
convenient to maintain a set of assumptions that cannot be violated
about certain types.
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 10 / 18
18. Static vs. Dynamic type system
is static or dynamic typing more convenient?
auto func = [] (int x) {
if (x >= 0) return 2*x;
return "negative number";
};
std::cout << func(2) << func(-1);
More convenient to write code in a dynamic language but more
convenient to maintain a set of assumptions that cannot be violated
about certain types.
does static typing prevent useful programs?
array t = { (1, 1), (true, true)}
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 10 / 18
19. Static vs. Dynamic type system
is static typings early bug-detection important?
static typing catches bugs earlier, when the code is statically checked,
compiled.
bugs are easier to fix if discovered sooner, while the developer is still
thinking about the code
the programmer can rely on the compiler and focus atention elsewhere
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 11 / 18
20. Static vs. Dynamic type system
does static or dynamic typing lead to better performance?
type tags do not exist at runtime, they take more space and slow down
constructors
faster code since it does not need to perform type tests at run time
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 12 / 18
21. Static vs. Dynamic type system
does static or dynamic typing lead to better performance?
type tags do not exist at runtime, they take more space and slow down
constructors
faster code since it does not need to perform type tests at run time
but if programmers in statically typed languages have to work around
type-system limitations, then those workarounds can erode the
supposed performance advantages
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 12 / 18
22. Static vs. Dynamic type system
is static or dynamic typing better for prototyping?
dynamic typing is often considered better for prototyping, no need to
define the types of variables, functions, and data structures when those
decisions are in flux
some part of the program would not type-check in a statically typed
language, but the rest of the program can run (e.g., to test the parts
you just wrote)
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 13 / 18
23. Static vs. Dynamic type system
is static or dynamic typing better for code evolution?
dynamic typing is sometimes more convenient for code evolution
because we can change code to be more permissive (accept arguments
of more types) without having to change any of the pre-existing clients
of the code.
f(x) = return 2 * x
static type-checking is very useful when evolving code to catch bugs
that the evolution introduces. When we change the type of a function,
all callers no longer type-check, which means the typechecker gives us
a ,,to-do list” of all the call-sites that need to change
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 14 / 18
24. Supports many paradigms
procedural programming (functions and data separately)
free functions not belonging to any class
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 15 / 18
25. Supports many paradigms
procedural programming (functions and data separately)
free functions not belonging to any class
OOP (functions + data grouped in classes)
multiple inheritance
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 15 / 18
26. Supports many paradigms
procedural programming (functions and data separately)
free functions not belonging to any class
OOP (functions + data grouped in classes)
multiple inheritance
Generic programming
algorithms running for any data type
algorithms run as fast as an algorithm tuned for a specific type
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 15 / 18
27. Supports many paradigms
procedural programming (functions and data separately)
free functions not belonging to any class
OOP (functions + data grouped in classes)
multiple inheritance
Generic programming
algorithms running for any data type
algorithms run as fast as an algorithm tuned for a specific type
functional programming
closures with imutable data
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 15 / 18
28. Supports many paradigms
procedural programming (functions and data separately)
free functions not belonging to any class
OOP (functions + data grouped in classes)
multiple inheritance
Generic programming
algorithms running for any data type
algorithms run as fast as an algorithm tuned for a specific type
functional programming
closures with imutable data
,,unsafe” programming
,,Trust the programmer”, the rational for C language.
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 15 / 18
29. When C++
Best suited for system applications:
must meet a constraint
really fast (simulation, computer generation of images)
minimal power
data layout
drivers
communication protocols, working with legacy systems
program size
static (including RTL)
dynamic (image and working set size)
efficient communication with outside entities
Hardware
OSes
code in other languages (C)
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 16 / 18
30. References
Scott Meyers (2014)
Why C++ Sails When the Vasa Sank
Moscow C++ Party.
Dan Grossman (2015)
Programming Languages
University of Washington.
Ben Karel (2009)
Sound and Complete
Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 17 / 18