4 No one size fits all Different projects have different constraints Hardware resources Reliability constraints Efficiency constraints Time Power Time to completion Developer skills Extremes All that matters is to get to the market first! If the program fails, people die A 50% overhead implies the need for another $50M server farm Stroustrup - C++11 Style - Mar'12 5

5 What we want A synthesis And integrated set of features C++11 is a significant improvement in that direction Articulated guidelines for use What I call style Stroustrup - C++11 Style - Mar'12 6

9 ISO C++11 This is a talk about how to use C++ well In particular, C++11 The C++ features as a whole support programming style This is not a talk about the new features in ISO C++11 I use those where appropriate My C++11 FAQ lists the new features Most of the C++11 features are already shipping E.g. Clang, GCC, and Microsoft C++ (the order is alphabetical ) The C++11 standard library is shipping E.g. Boost, Clang, GCC, Microsoft C++ Stroustrup - C++11 Style - Mar'12 10

13 Unfair? No! I didn t make up that example it is repeatedly offered as an example of good code (for decades) qsort() is a popular ISO C standard-library function That qsort() code is readable compared to most low-level C/C++ code The style is not uncommon in production code Teaching and academic versions often simplify to protect the innocent (fraud?) I see much worse on bulletin boards Have a look, and cry Many students aim for that level of code for efficiency because it is cool (their idols does/did it!) It s not just a C/C++ problem/style Though I see C and C-style teaching as the source of the problem Stroustrup - C++11 Style - Mar'12 14

14 Does it matter? Yes! Bad style is the #1 problem in real-world C++ code Makes progress relatively easy Only relatively easy: bad code breeds more bad code Lack of focus on style is the #1 problem in C++ teaching A hack is usually the quickest short-term solution Faster than thinking about design Many teach poor style Many are self-taught Take advice from Decades old books Other novices Imitate Other languages Bad old code Stroustrup - C++11 Style - Mar'12 15

15 So what do I want? Simple interfaces void sort(container&); Simple calls vector<string> vs; // sort(vs); // this, I can do // for any container (e.g. vector, list, array) // I can t quite get this is in C++ (but close) Uncompromising performance Done: std::sort() beats qsort() by large factors (not just a few percent) No static type violations Done No resource leaks Done (without a garbage collector) Stroustrup - C++11 Style - Mar'12 16

33 Resource Management Style Prefer classes where the resource management is part of their fundamental semantics E.g., std::vector, std::ostream, std::thread, Use smart pointers to address the problems of premature destruction and leaks std::unique_ptr for (unique) ownership Zero cost (time and space) std::shared_ptr for shared ownership Maintains a use count But they are still pointers any pointer is a potential race condition even in a single threaded program Stroustrup - C++11 Style - Mar'12 34

34 How to move a resource Common problem: How to get a lot of data cheaply out of a function Idea #1: Return a pointer to a new d object Matrix* operator+(const Matrix&, const Matrix&); Matrix& res = *(a+b); // ugly! (unacceptable) Who does the delete? there is no good general answer Stroustrup - C++11 Style - Mar'12 35

35 How to move a resource Common problem: How to get a lot of data cheaply out of a function Idea #2 Return a reference to a new d object Matrix& operator+(const Matrix&, const Matrix&); Matrix res = a+b; // looks right, but Who does the delete? What delete? I don t see any pointers. there is no good general answer Stroustrup - C++11 Style - Mar'12 36

36 How to move a resource Common problem: How to get a lot of data cheaply out of a function Idea #3 Pass an reference to a result object void operator+(const Matrix&, const Matrix&, Matrix& result); Matrix res = a+b; // Oops, doesn t work for operators Matrix res2; operator+(a,b,res2); // Ugly! We are regressing towards assembly code Stroustrup - C++11 Style - Mar'12 37

37 How to move a resource Common problem: How to get a lot of data cheaply out of a function Idea #4 Return a Matrix Matrix operator+(const Matrix&, const Matrix&); Matrix res = a+b; Copy? expensive Use some pre-allocated result stack of Matrixes A brittle hack Move the Matrix out don t copy; steal the representation Directly supported in C++11 through move constructors Stroustrup - C++11 Style - Mar'12 38

43 Vector vs. List Generate N random integers and insert them into a sequence so that each is inserted in its proper position in the numerical order gives: Remove elements one at a time by picking a random position in the sequence and removing the element there. Positions gives For which N is it better to use a linked list than a vector (or an array) to represent the sequence? Stroustrup - C++11 Style - Mar'12 44

45 Vector vs. List Find the insertion point Linear search Vector could use binary search, but I did not Insert List re-links Vector moves on average n/2 elements Find the deletion point Linear search Vector could use direct access, but I did not delete List re-links Vector moves on average n/2 elements Allocation List does N allocations and N deallocations The optimized/preallocated list do no allocations or dealloations Vector does approximately log2(n) allocations and log2(n) deallocations The optimized list does 1 allocation and 1 deallocation This completely dominates Stroustrup - C++11 Style - Mar'12 46

49 Algorithms vs. Code Problem: drag item to an insertion point Original solution (after cleanup and simplification): 25 lines of code one loop three tests 14 function calls Messy code Is it correct? who knows? try lots of testing Is it maintainable? Probably not, since it is hard to understand Is it usable elsewhere? No, it s completely hand-crafted to the details of the problem The author requested a review Professionalism! Stroustrup - C++11 Style - Mar'12 50

57 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 - C++11 Style - Mar'12 58

59 Type-Safe Concurrency Programming concurrent systems is hard We need all the help we can get C++11 offers type-safe programming at the threads-and-locks level Type safety is hugely important threads-and-locks is an unfortunately low level of abstraction is necessary for current systems programming That s what the operating systems offer presents an abstraction of the hardware to the programmer can be the basis of other concurrency abstractions Stroustrup - C++11 Style - Mar'12 60

Interpreters and virtual machines Michel Schinz 2007 03 23 Interpreters Interpreters Why interpreters? An interpreter is a program that executes another program, represented as some kind of data-structure.

The original English text of an interview with the Russian news organization CNEWS: C++ future Soon we will have a new standard for C++. What is going to change in this programming language? What new features

Compiling Object Oriented Languages What is an Object-Oriented Programming Language? Last time Dynamic compilation Today Introduction to compiling object oriented languages What are the issues? Objects

Why you shouldn't use set (and what you should use instead) Matt Austern Everything in the standard C++ library is there for a reason, but it isn't always obvious what that reason is. The standard isn't

The programming language C sws1 1 The programming language C invented by Dennis Ritchie in early 1970s who used it to write the first Hello World program C was used to write UNIX Standardised as K&C (Kernighan

Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed Computing Netprog 2002 Java Intro 1 What is

Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction

Chapter 6 Variable Base Interface 6.1 Introduction Finite element codes has been changed a lot during the evolution of the Finite Element Method, In its early times, finite element applications were developed

INTRODUCTION 1 Programming languages have common concepts that are seen in all languages This course will discuss and illustrate these common concepts: Syntax Names Types Semantics Memory Management We

Curriculum Map Discipline: Computer Science Course: C++ August/September: How can computer programs make problem solving easier and more efficient? In what order does a computer execute the lines of code

PROBLEM SOLVING WITH SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON Addison Wesley Boston San Francisco New York London

N3458: Simple Database Integration in C++11 Thomas Neumann Technische Univeristät München neumann@in.tum.de 2012-10-22 Many applications make use of relational database to store and query their data. However,

CORBA Programming with TAOX11 The C++11 CORBA Implementation TAOX11: the CORBA Implementation by Remedy IT TAOX11 simplifies development of CORBA based applications IDL to C++11 language mapping is easy

Jonathan Worthington Scarborough Linux User Group Introduction What does a Virtual Machine do? Hides away the details of the hardware platform and operating system. Defines a common set of instructions.

OpenACC 2.0 and the PGI Accelerator Compilers Michael Wolfe The Portland Group michael.wolfe@pgroup.com This presentation discusses the additions made to the OpenACC API in Version 2.0. I will also present

Organization of Programming Languages CS320/520N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Names, Bindings, and Scopes A name is a symbolic identifier used

Every software developer deals with bugs. The really tough bugs aren t detected by the compiler. Nasty bugs manifest themselves only when executed at runtime. Here is a list of the top ten difficult and

An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf C++ is a legacy programming language, as is other languages

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C 1 An essential part of any embedded system design Programming 2 Programming in Assembly or HLL Processor and memory-sensitive

Code Refactoring and Defects Reusing this material This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License. http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_us

C/C++ Refresher Session Michael DiBernardo and Lloyd Markle September 18th, 2007 You always hurt the one you love If C++ has taught me one thing, it's this: Just because the system is consistent doesn't

C++ for Safety-Critical Systems DI Günter Obiltschnig Applied Informatics Software Engineering GmbH guenter.obiltschnig@appinf.com A life-critical system or safety-critical system is a system whose failure

LECTURE 12 Some Tips on How to Program in C++ When it comes to learning how to program in C++, there's no substitute for experience. But there are some things that are good to keep in mind. Advice on how

Fundamentals of Java Programming This document is exclusive property of Cisco Systems, Inc. Permission is granted to print and copy this document for non-commercial distribution and exclusive use by instructors

Any clod can have the facts, but having opinions is an art. Charles McCabe, San Francisco Chronicle Open Channel In Design and Evolution of C++ (Addison Wesley, 1994), Bjarne Stroustrup argued that a programming

Chapter 1 Coding conventions and C++-style This document provides an overview of the general coding conventions that are used throughout oomph-lib. Knowledge of these conventions will greatly facilitate

Objectif This six-day instructor-led course provides students with the knowledge and skills to develop applications in the.net 3.5 using the C# 3.0 programming language. C# is one of the most popular programming

Course 10550A: Programming in Visual Basic with Microsoft Visual Studio 2010 OVERVIEW About this Course This course teaches you Visual Basic language syntax, program structure, and implementation by using

Java Application Developer Certificate Program Competencies After completing the following units, you will be able to: Basic Programming Logic Explain the steps involved in the program development cycle

We SOLVE COMPLEX PROBLEMS of DATA MODELING and DEVELOP TOOLS and solutions to let business perform best through data analysis Whitepaper: performance of SqlBulkCopy This whitepaper provides an analysis

Keywords Compared in Various Languages Visual Studio 2010 This topic lists common programming tasks that can be summarized with a language keyword. For more information about tasks that need code examples,

1 The Java Virtual Machine About the Spec Format This document describes the Java virtual machine and the instruction set. In this introduction, each component of the machine is briefly described. This