C++

Use AddressSanitizer.

C++23

C++23 - cppreference.com

import std;

int main()
{
    std::println("Hello, world!");
}

C++23's New Fold Algorithms - C++ Team Blog

Courses

Great talks

Links

Snippets

For loop with index

std::vector<std::string> items { "a", "b", "c" };
for (size_t index = 0; const auto &item : items) {
	std::cout << item << " at index " << index << std::endl;
	index += 1;
}

Functional vector map

std::vector<int> numbers { 1, 2, 3 };
std::vector<std::string> strings;
std::transform(
    numbers.cbegin(), numbers.cend(), std::back_inserter(strings),
    [](const auto &n) { return "Number " + std::to_string(n * 10); });

See ranges as well.

Dealing with smart pointers

#include <memory>

class Person {};

class Wrapper {
public:
  explicit Wrapper(Person *person) : _person(person) {}
private:
  Person *_person;
};

class RefWrapper {
public:
  explicit RefWrapper(Person &person) : _person(person) {}
private:
  // storing references prevents copying
  Person &_person;
};

void handle_person(const Person &person) {}

int main() {
  std::unique_ptr<Person> person = std::make_unique<Person>();

  Wrapper wrapper{person.get()};

  RefWrapper refWrapper{*person};

  handle_person(*person);
}

Common mistakes

  • Vector reallocation on resize will invalidate iterators

Tools

Interesting codebases to read