Examples and observations

#include <numeric>
#include <vector>
#include <fmt/core.h>
#include <fmt/ranges.h>

void align() {

    struct S {
        uint8_t : 8;
        uint32_t x;
    } __attribute__((packed));

    fmt::print("alignof {} sizeof {}\n", alignof(S::x), sizeof(S::x));
    fmt::print("alignof {} sizeof {}\n", alignof(S), sizeof(S));

void print_container() {

    std::vector<uint32_t> v(10);
    std::iota(begin(v), end(v), 3);

    fmt::print("{}\n", v);

int main() {


alignof 1 sizeof 4
alignof 1 sizeof 5
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

C++ memory alignment

#include <fmt/core.h>

int main() {

    struct S {
        uint8_t : 8;
        uint32_t x;
    } __attribute__((packed));

    fmt::print("alignof {} sizeof {}\n", alignof(S::x), sizeof(S::x));
    fmt::print("alignof {} sizeof {}\n", alignof(S), sizeof(S));


alignof 1 sizeof 4
alignof 1 sizeof 5

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. – const and static variables don’t have external linkage; non-const global variables have external linkage by default; const global variables have internal linkage by default Variables with static storage duration are zero initialised. According to the standard: “All objects which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration”. [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]