#til notes

Random bits I posted on twitter or wrote down for myself. Last update 2018-03-17

C++: you can omit parentheses in lambda expression if the lambda takes no arguments; auto lambda = []{}; is valid

std::regex doesn't hold the string it was built from

C++: the final keyword doesn't imply override.

Macros don't go with templates.

#include <map>
#define decl(name, type) type name

decl(kitten, int); // OK
decl(nope, std::map<int,bool>); // error: macro "decl" passed 3 arguments, but takes just 2

It's possible to directly compare an optional with a value, like std::optional<T>() == T().

C++11: although it's possible to pass by reference this pointer to a lambda and return such lambda outside the class, the reference becomes invalid when instance is destroyed.

#include <functional>
#include <string>
#include <cstdlib>

struct Foo {

    int counter;
    int* ptr;

    Foo() : ptr(&counter) {}
    ~Foo() {
        ptr = nullptr;

    std::string s;

    std::function<void()> getfun() {
        return [this]() {
            *ptr += 1;

int main() {

    std::function<void()> fun;

        Foo f{};
        fun = f.getfun();

    // program prints "destroyed" and ...
    fun(); // ... segfaults here

C++ odds: there's no name clash between variables and classes; following program is perfectly valid:

int foo;
class foo {};

GCC provides useful cpp-related warnings (not enabled by -Wall): -Wsuggest-final-types, -Wsuggest-final-methods, -Wsuggest-override

C++17: std::regex doesn't hold the string it was built from; pretty unhandy feature

C++: GCC 7/clang 5, invoked with at least -O2, remove at() invocation in such a loop:

for (size_t i=0; i < vec.size(); i++)

C++17: std::move is impossible on std::reference_wrapper

C++17: std::filesystem::remove_all(path) asserts that the path exists and throws an exception otherwise; boost::filesystem::remove_all does nothing in such case

SQLite: foreign keys have to be enabled manually by command PRAGMA foreign_key = on

C++17: std::optional doesn't accept references, while boost::optional does

C++: std::unique_ptr<T>(nullptr) == nullptr yields true

SSE intrinsics: _mm_extract_ps returns an integer, the suffix "ps" suggests a float

C++: std::list::size() is constant time since C++11

MSVC doesn't support a warning directive; instead it has a pragma: message("text")

C++: sizeof(type*) == sizeof(std::unique_ptr<type>)

C++: std::unordered_map has got method at()

GCC/clang: -Wfatal-errors stops compilation on the first error