Get a std::move on

Move semantics in C++

  • You cannot move from a const thing: not allowed to mutate
  • Auto-generated constructors do not have strong exception guarantees
  • Adding a custom empty destructor prevents the move constructors being generated automatically
  • Fundamental types are copied when moved as they don’t handle resources
  • Move is used when returning from a function or we make a variable an rvalue with std::move
  • && modifier indicates parameter is an object that we intend to move from instead of copying

Using type traits to check all the esoteric OOP rules

#include <iostream>
#include <vector>
#include <memory>
#include <type_traits>

struct A {
    virtual ~A(){ std::cout << "~A\n"; }
    int i;
    int j;

struct B : A {
    ~B(){ std::cout << "~B\n"; }


int main() {
    std::vector<std::unique_ptr<struct A>> vec;



Notes from Andrei at code::dive 2015

Watch the whole lecture (recommended). Integrals Prefer 32-bit ints to other sizes 32-bits is the sweet spot as 64-bit ALU can handle two calculations 8, 16-bit computations convert to 32-bits so don’t use smaller Use small ints in arrays Prefer unsigned to signed Unsigned is faster Except when converting to floating point Most numbers are small If you find optimisations that work with small numbers, use them Floating point Double and single precision equivalent speed 80-bit extended only slightly slower But don’t mix them (because conversions) Ints to float point cheap Floating point to any integral is expensive Strength reduction Use minimum strength operations when optimising as the stronger ones are more costly. [Read More]

Virtual insanity...

Uses of the "virtual" keyword in C++

Virtual functions are used to achieve runtime polymorphism – Uses of the virtual keyword See Related keywords: override, final virtual function specifier The virtual specifier specifies that a non-static member function is virtual and supports dynamic dispatch. It may only appear in the decl-specifier-seq of the initial declaration of a non-static member function (i.e., when it is declared in the class definition). Virtual functions are member functions whose behavior can be overridden in derived classes. [Read More]

Coding without coding

Tools that will improve your code so you don't have to

  • iwyu: tidy includes to improve build times
  • clang-format: format your code
  • clang tidy

See also (Jason Turner)

  • reddit/r/cpp
  • C++ ISO home
  • (c++ section)
  • – compiler’s view of code
  • – search c++ standard
  • – up to the minute standard for the C++ programming language
  • (also includes a C reference and offline archive)


  • Fluent C++ (blog) – include daily tutorial for teams
  • C++ stories
  • Sutter’s Mill
  • Random ASCII by Bruce Dawson
  • Modernes C++
  • The Old New Thing (Raymond Chen)


  • Jason Turner
  • The Cherno
  • cppcon
  • bisqwit
  • OneLoneCoder
  • Meeting Cpp (conference channel)

Profiling resources


  • valgrind
  • perf
  • /usr/bin/time --verbose
  • Tracy
  • htop
  • Google Benchmark
  • gprof

Graph tools

Compiler options

Enabling in gcc

Use -p for prof and -pg for gprof. But it will take longer to run.



Dependencies on static variables in different translation units are, in general, a code smell and should be a reason for refactoring. – Non-const global variables have external linkage by default Const global variables have internal linkage by default Functions have external linkage by default What’s the “static initialization order ‘fiasco’ (problem)”? C++ scoped static initialization is not thread-safe, on purpose! (pre-C++11) constinit static vs std::call_once vs double checked locking DCLP: Double-Checked Locking Pattern const and static variables don’t have external linkage. [Read More]

Favourite C++ features

And wish list

C++17 Boost filesystem execution policy: parallel algorithm support for range-based for loops - potential for easily parallelsing existing code structured bindings clamp std::optional [[maybe_unused]] C++20 Concepts Concepts are a revolutionary approach for writing templates! They allow you to put constraints on template parameters that improve the readability of code, speed up compilation time, and give better error messages. C++20 gives us language support (new keywords - requires, concept) and a set of predefined concepts from the Standard Library. [Read More]

C++ recipes

Operating over a whole container using ranges (C++20) #include <vector>#include <algorithm>#include <random>#include <iostream> int main() { const auto printy = [](const auto &vec){ for (size_t i = 0; const auto&v : vec) std::cout << v << (++i == vec.size() ? "\n" : " "); }; std::vector<int> v(4); printy(v); std::ranges::fill(v, -1); printy(v); std::ranges::generate(v, std::rand); printy(v); } Generates: 0 0 0 0 -1 -1 -1 -1 1804289383 846930886 1681692777 1714636915 Zip two containers #include <iostream>#include <vector>#include <algorithm>#include <ranges> int main() { const std::vector<int> a{1, 2, 3}; const std::vector<int> b{4, 5, 6}; std::vector<std::pair<int, int>> zipped; zipped. [Read More]


Debugger crib sheet

Adding debug symbols gcc -g hello.c gdb a.out # Run program run # Run and immediately break start # View code at current break point list # Break main b main # Break at line 9 b 9 TUI mode - text user interface Type ctrl-X A ctrl L - repaint ctrl X2 - multiple windows ctrl X2 - view registers tui reg float - view floating registers ctrl x 1 - source code view ctrl p/n - command history Python interpreter built in (since version 7) import os print(os. [Read More]