31
C++ by Choice Florentin Picioroaga IDS GmbH, Ettlingen, Germany [email protected] February 24, 2016 Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 1 / 18

Florentin Picioroaga - C++ by choice

Embed Size (px)

Citation preview

Page 1: Florentin Picioroaga - C++ by choice

C++ by Choice

Florentin Picioroaga

IDS GmbH, Ettlingen, Germany

[email protected]

February 24, 2016

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 1 / 18

Page 2: Florentin Picioroaga - C++ by choice

Overview

1 Motivation

2 Why C++?PopularityGeneral featuresStatic vs. Dynamic type systemSupports many paradigms

3 When C++?

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 2 / 18

Page 3: Florentin Picioroaga - C++ by choice

Motivation

Why C++, when there are so many programming languages?

When C++? What is the best match (application, programminglanguage = C++)?

Not in the scope:

Why not C++?When not C++?

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 3 / 18

Page 4: Florentin Picioroaga - C++ by choice

Motivation

Why C++, when there are so many programming languages?

When C++? What is the best match (application, programminglanguage = C++)?

Not in the scope:

Why not C++?When not C++?

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 3 / 18

Page 5: Florentin Picioroaga - C++ by choice

Motivation

Why C++, when there are so many programming languages?

When C++? What is the best match (application, programminglanguage = C++)?

Not in the scope:

Why not C++?When not C++?

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 3 / 18

Page 6: Florentin Picioroaga - C++ by choice

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

Page 7: Florentin Picioroaga - C++ by choice

Popularity

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 5 / 18

Page 8: Florentin Picioroaga - C++ by choice

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

Page 9: Florentin Picioroaga - C++ by choice

General features

General-purpose undo mechanism with destructors

Undo for: memory, mutexes, opening filesRAII, implemented in C++ with constructors/destructorsGuaranteed to be executed even when an exception occurs, only for theobjects on the stackDeterministic finalization in contrast with GC systems

templates

generic programming (STL)TMP (Template Meta-Programming)

overloading

function objects, the basis of lambdas in C++11smart pointers

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 7 / 18

Page 10: Florentin Picioroaga - C++ by choice

General features

General-purpose undo mechanism with destructors

Undo for: memory, mutexes, opening filesRAII, implemented in C++ with constructors/destructorsGuaranteed to be executed even when an exception occurs, only for theobjects on the stackDeterministic finalization in contrast with GC systems

templates

generic programming (STL)TMP (Template Meta-Programming)

overloading

function objects, the basis of lambdas in C++11smart pointers

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 7 / 18

Page 11: Florentin Picioroaga - C++ by choice

General features

General-purpose undo mechanism with destructors

Undo for: memory, mutexes, opening filesRAII, implemented in C++ with constructors/destructorsGuaranteed to be executed even when an exception occurs, only for theobjects on the stackDeterministic finalization in contrast with GC systems

templates

generic programming (STL)TMP (Template Meta-Programming)

overloading

function objects, the basis of lambdas in C++11smart pointers

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 7 / 18

Page 12: Florentin Picioroaga - C++ by choice

General features

General-purpose undo mechanism with destructors

Undo for: memory, mutexes, opening filesRAII, implemented in C++ with constructors/destructorsGuaranteed to be executed even when an exception occurs, only for theobjects on the stackDeterministic finalization in contrast with GC systems

templates

generic programming (STL)TMP (Template Meta-Programming)

overloading

function objects, the basis of lambdas in C++11smart pointers

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 7 / 18

Page 13: Florentin Picioroaga - C++ by choice

Static vs. Dynamic type system

from formal logic we have two properties we can use to describe anyevaluation procedure: soundness and completeness

a sound type system is one that rejects all erroneous programs, and acomplete system accepts all correct programs

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 8 / 18

Page 14: Florentin Picioroaga - C++ by choice

Static vs. Dynamic type system

from formal logic we have two properties we can use to describe anyevaluation procedure: soundness and completeness

a sound type system is one that rejects all erroneous programs, and acomplete system accepts all correct programs

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 8 / 18

Page 15: Florentin Picioroaga - C++ by choice

Static vs. Dynamic type system

a programming language with a complete and unsound (static) typesystem 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 aprogram 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-typedJavaScript running the user interfacemany large video games, e.g. World of WarCraft: C++ and Lua

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 9 / 18

Page 16: Florentin Picioroaga - C++ by choice

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 moreconvenient to maintain a set of assumptions that cannot be violatedabout 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

Page 17: Florentin Picioroaga - C++ by choice

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 moreconvenient to maintain a set of assumptions that cannot be violatedabout 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

Page 18: Florentin Picioroaga - C++ by choice

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 moreconvenient to maintain a set of assumptions that cannot be violatedabout 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

Page 19: Florentin Picioroaga - C++ by choice

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 stillthinking about the codethe programmer can rely on the compiler and focus atention elsewhere

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 11 / 18

Page 20: Florentin Picioroaga - C++ by choice

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 downconstructorsfaster code since it does not need to perform type tests at run time

but if programmers in statically typed languages have to work aroundtype-system limitations, then those workarounds can erode thesupposed performance advantages

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 12 / 18

Page 21: Florentin Picioroaga - C++ by choice

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 downconstructorsfaster code since it does not need to perform type tests at run timebut if programmers in statically typed languages have to work aroundtype-system limitations, then those workarounds can erode thesupposed performance advantages

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 12 / 18

Page 22: Florentin Picioroaga - C++ by choice

Static vs. Dynamic type system

is static or dynamic typing better for prototyping?dynamic typing is often considered better for prototyping, no need todefine the types of variables, functions, and data structures when thosedecisions are in fluxsome part of the program would not type-check in a statically typedlanguage, but the rest of the program can run (e.g., to test the partsyou just wrote)

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 13 / 18

Page 23: Florentin Picioroaga - C++ by choice

Static vs. Dynamic type system

is static or dynamic typing better for code evolution?dynamic typing is sometimes more convenient for code evolutionbecause we can change code to be more permissive (accept argumentsof more types) without having to change any of the pre-existing clientsof the code.

f(x) = return 2 * x

static type-checking is very useful when evolving code to catch bugsthat the evolution introduces. When we change the type of a function,all callers no longer type-check, which means the typechecker gives usa ,,to-do list” of all the call-sites that need to change

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 14 / 18

Page 24: Florentin Picioroaga - C++ by choice

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 typealgorithms 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

Page 25: Florentin Picioroaga - C++ by choice

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 typealgorithms 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

Page 26: Florentin Picioroaga - C++ by choice

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 typealgorithms 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

Page 27: Florentin Picioroaga - C++ by choice

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 typealgorithms 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

Page 28: Florentin Picioroaga - C++ by choice

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 typealgorithms 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

Page 29: Florentin Picioroaga - C++ by choice

When C++

Best suited for system applications:

must meet a constraint

really fast (simulation, computer generation of images)minimal powerdata layout

driverscommunication protocols, working with legacy systems

program size

static (including RTL)dynamic (image and working set size)

efficient communication with outside entities

HardwareOSescode in other languages (C)

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 16 / 18

Page 30: Florentin Picioroaga - C++ by choice

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

Page 31: Florentin Picioroaga - C++ by choice

Hope you enjoyed!

Florentin Picioroaga (IDS GmbH) C++ by Choice February 24, 2016 18 / 18