CppCon 2019: Maintainability and Refactoring Impact of Higher-Level Design Features

Titus Winter

Multi-step refactoring

  • Basic atoms of refactoring

Rename a type

namespace oldns {class StringPiece ...}
namespace absl {class string_view ...copy StringPiece }


namespace oldns {using StringPiece = absl::string_view}

break with forward declaration. forward declarations no real performance improement

also, ADL causes it to break


namespace oldns {class StringPiece...}
namespace absl { using string_view = oldns::StringPiece;}

then deal with ADL problems

Find a way tso pre/post syntax/semancs can co-exist

Can we change functions?

  • small atoms: weaken preconditions; stregthen postconditions
  • large atomes: opposite

Changing return types

  • void to anything is doable
  • into to int64_t?

Can we change templates

Can we change concepts

  • Single-step: sure
  • Multi-step: No

CppCon 2019: Speed Is Found In The Minds of People

Andrei Alexandrescu

This was the best talk I saw at CppCon 2019


  • What is quick-select?

Naive Implementations

  • qsort()


  • qsort() with fallback to small_sort()

Investigating small_sort

  • Challenge: increase the THRESHOLD so sorting for 1000 elements work
  • binary search is slower. Branch prediction is reader
  • branchless even slower than binary
  • middle-out insertion support: better comparison and better moves (13%) but no performance change
  • stupid_small_sort(): make_heap(); insertion_sort(). Improvment in comparison, swaps; 9% slower
  • Blended cost: (C(N) + M(n) + k(D(n))/n. (compares + swaps + distance)


  • 1990’s OOP
    • Inheritance and virtuals
  • 2000’s Generic Programing
    • Iterators and algos
  • 2020’s: design by introspection
    • Inspect and customize everything
    • We can’t implement the best sort with generic programming


  • Parametric complexity theory???

CppCon 2019: RAII and the Rule of Zero

Arthur O’Dwyer

Naive Vector

  • Use copy and swap for assignment. Example of recursive shared_ptr in Naivevector

NaiveVector &operator=(const NaiveVector &op2)
     NaiveVectory copy = op2;
     return *this;

  • RAII and exception safety
    • struct RAIIPtr{} example
    • rule of three
    • We don’t want RAIIPtr to be copyable or assignamle

RAIIPTR(const RAIIPTR &) = delete;
RAIIPtr&operator=(const RAIIPtr &op2) = delete;

  • Defaulted special member functions
    • help your code to be self documenting

Rule of Three

If need a destructor then need copy and assignment operators

Rule of Zero

If your class does not directly manage any resource then strive to write no special member functions

But your own swap might improve performance

Introducing rvalue references

As a general rule, lvalue reference parameters do not bind to rvalues and rvalue reference parameters do not bind to lvalues. A const ref will bind to rvalue

Move constructor

Rule of Five

  • A destructor
  • copy constructor
  • move constructor to transfer ownership (for performance)
  • copy assignment operator
  • move assignment operator (for performance)

Copy-and-swap leads to duplication

NaiveVector& NaiveVector:operator=(NaiveVector &&rhs){
    NaiveVector copy(std::move(rhs));
    return *this

By-value assignment operator

NaiveVector &operator=(NaiveVector copy)
   return *this;

Rule of Four (and a half)

  • destructor
  • copy constructor
  • move constructor
  • by-value assignment operator
  • 1/2 (A nonmember swap() function, and idealy a member version too)

No longer

Cluster to rule of zero

  • Use std::unique_ptr() and we can default the move constructor
  • Other constructors are fine (including Allocator)

Examples of resource management

  • unique_ptr
  • shared_ptr
  • unique_lock
  • ifstream

CppCon 2019: C++ Modules: Guides for The Working Software developer

Gabriel Dos Reis

<h1>existing practice and Challenges</h1>
  • build time scalability of idiomatic C++

Fragile and Outdated Linking Mode

Program = collection of independently translated source files

Macros are a big problem

Modules accepted for C++20

What did we accomplish

  • Languate notions and constructs
    • Componentization
    • Isolation
    • Build throughput
    • Semantics-aware developer tools
  • With transition paths
    • Global module fragments
    • Header units

Module Definition (101)

export module BasicPlane.Figures;
export struct X ...

#include <iostream>
import BasicPlane.Figures;

How does it work?

  • The generated module generates .o plus metadata
  • If name is not exported it is not available

Efficient Pimpl

  • module :private;

Spreading Module Definition

  • “export module” gives module definition
  • “module X” is implementation

Spreading Module Interface

  • Primary module interface

export module BasicPlane.Figures;
export import :PointPart;
export import :RectPart;

export module BasicPlane.Figures:PointPart;
export struct Point {...};

export module BasicPlane.Figures:RectPart;
import :PointPart;
export struct Rectangle {...};
export int width(const Rectangle &); ...

Module Aggregation

export module BasicPlane;

export import BasicPlane.Figures;
export import BasicPlane.Transformations;
export import BasicPlane.Canvas;

Transitioning into modular world, and preparing for modules

#include <sys/stat.h>

export module System.File.Ops;
namespace Sys {
    export stat stat_dierctory(const char *);

    export using ::chmod;

Heder Unit: An almost module

import <iostream>
import BasicPlane.Figures;

Kicking the Tires with MSVC, GCC, and clang



  • Does
    import <iostream>

    obsolete pre-compiled headers? Yes

  • PCH vs usual include. 10 times faster

CppCon 2019: The basics of Move Semantics

Klaus Iglberger

  • Value semantics example with vector
  • v2 = createVector()
  • vs = std::move(v1);
  • simple defn of rvalue, lvalue: lvalue has a name you give it
  • operator=(vector&&rhs) rvalue reference
  • Xvalue: expiring value
  • do not use the moved from value (the xvalue)

New speicial member functions

  • Move constructor
    • Widget (Widget &&w) = default
  • Move assignment operator
    • Widget &operator=(Widget &&w) = default
  • “Rule of zero” if you can avoid defining default operations, good

Move constructor

Widget(Widget &&w)
: i(w.i)
, s (w.s) // bad!

Widget(Widget &&w) noexcept
: i(std::move(w.i)) // not necessary but consistent
, s(std::move(w.s))
, pi(std::move (w.pi))
    w.pi = nullptr;

, pi(std::exchange(w.pi, nullptr))

  • Make move operations noexcept (Core Guidelines)

Move assignment operator

  • clean up all visible resources
  • Transfer the content of w into this
  • Leave w in a valid but undefined state
  • w = std::move(w);
  • phase 1: cleanup
  • phase 2: member-wise move
  • phase 3:


  • The default move operations are generate if no copy operation or destructor is user-define
  • The default copy operations are generated if no move operation is user-defined
  • Note: = default and =delete count as user-defined
  • Rule of five or rule of six: if you define or =delete any default operation define or =delete them all.

CppCon 2019: The C++20 Standard Library – Beyond Ranges

Jeff Garland



Not about ranges

  • Up to date with respect to Cologne
  • Summary
  • boost datetime


  • io – formatted output (15min)
  • container and algorith updates (12min)
  • chrono (8min)
  • concurrency (10min)
  • misc(5min)
  • bit manip (5min)

Not goals

  • ranges
  • concept
  • spaceship
  • concurrency other topics
  • char8_t, u8string_view, etc
  • constexpr, …



  • python style formatting
  • faster and less overhead
  • string s = fmt::format(“{} there {}\n”, “hello”, “world”);
  • floating point
  • rich languate for fill, alignment
  • custom types
    • template specialization
  • fmt diagnostics/saefty
  • runtime diagnostic (std::exception)
  • wg21.link/p0645
  • https://github.com/fmtlib/fmt

syncbuf and osyncstream

stringstream and basic_stringbuf

  • control of buffer allocator
  • stringstream.view() so no need to copy string
  • p0408

container and algorithm

  • span view over contiguous seqnce
  • cheap to copy
  • constant time complexity
  • mutable
  • P0122
  • github.com/tcbrindle/span
  • contains()

    for associative containers

  • p0458
  • uniform container erasure
  • library fundamentals v2


  • structure/field types like year_month_day
  • calculation types ike sys_days, time_point, seconds
  • field types sed for i/o and conversions
  • no exceptions
  • fmt i/o
  • https:://github.com/HowardHinnand/date
  • p0355


  • jthread and stop_token
  • atomic<shared_ptr> and weak_ptr<>
    • p0718
  • jthread automatically joins in destructor
  • stop_token provides cooperator shutdown
  • p0660 (wording) p0660r0 (motivation)
  • github.com/josuttis/jthread
  • atomic_ref


  • string_view starts_with() and ends_with()
  • source_location
    • modern replace for FILE and LINE
    • static consteval source_location current() noexcpt;
    • p1208
  • math constants
    • <math>
    • p0631
    • 1midpoint()` difficult
  • bit manipulation
    • <bit>
    • p0553
  • bit_cast
    • p0476
  • endian
    • p0463

CppCon 2019: C++-20 at 40

Bjarne Stroustrup

  • Introduced b a high school student

  • Stability and evolution
  • “A tour of C++”, 2nd edition
  • Distinguish betwee what’s lega and what’s effective
    • Better tool support, eg C_+ core Guidlines
  • C++: principled and eclectic
  • C++ a general purose programming language for the defn, impl and use of lightweight abstractions
  • “We don’t really like C++ but is it the only thing that works”
  • C++ high level aims
    • Evolutionary (stable, gradual adoption)
    • Make simple things simple
    • Zero-overhead principle
    • Aim high: change the way we design and implement software and we think
  • Key C++ “Rules of thumb”
    • Static type sisteym build-in and user-defined
    • Value and referenc semantics
    • Direct use of machine and operating system resources
      • Layers of abstraction
        • The Onion principle:The more layers you peel off, the more you cry
    • Systematic and generl resource management (RAII)
      • Construct/destructor paris
      • Every resource must have an owner; don’t use built-in pointers (T*) to manage
    • Support composition of software from separatelhy developed parts
      • Modules
    • Support for generic programming
      • write code that works for types tat meet abstract requirements
      • requiremensts are defined as concepts
    • Support fo compile-time programming
      • const_expr
      • Move computaton from run-time to compile-time
    • Support for object-oriented programming
      • Still useful and popular
      • Need reference semantics
      • Don’t need run-time; use static resolution (variant)
      • See

        which do not ake it into c++20

    • Concurrency through libraries supported by intrinsics
      • mutex, RAII
      • atomics
    • C++ is tunable and evolves
      • Always measure
    • Things work in combination
    • Library design is language desing; language design is library design
    • cppreference.com/w/cpp/libs
    • Chrono
    • You have to raise the level abstraction for tools to enforce type and resource-safe C++
    • These rules are about 40 years old
    • C++2o the best approximation of C++’s ideals (so far)
  • The future
    • C++23:
      • Completes C++20
      • Plus: standard modules, library support for coroutines, executors and netowrking
      • Maybe: static reflection, pattern match
  • Questions
    • Something about co_await and threads
    • How to deal with legacy code
      • In new code use modern features
      • Either try and encapsulate
      • Need more tooling
    • What feature most affected c++11:
      • const_expr
      • basic support for concurrent programming
    • What feature most affected c++20:
      • concepts will make generic much simpler, elegant, adn useful
      • modules should improve our compile speeds (5-10 times?). Major advantage is from cleaning up your code.
    • Not pay for things you use. However someone has to pay $ for implementing these features
      • It’s better to put the burden on implementers then users. 50,000 users for 5-10 implementors
    • Are there plans to add features to enforce ownership checks at compile time?
      • Use static enforced coding guideless to enforce “good” coding standards instead of “legal” codes.
    • Cyril from Bloomberg. Would you discourage use of runtime polymorphism?
      • Does someting really need hierarchis and runtime/dynamic types