Meeting 30 May 2019

What was your latest discovery about C++?

URLs in code are legal

void fn() {
    cout << "Everything is fine.\n"
int x = 10;
while (x --> 0) // x goes to 0
    printf("%d ", x);

For every type T the unary operator+(T*) is considered to exist which returns the given pointer as is. Here, T is not restricted to object types but includes function types. A lambda object that didn't capture anything has a conversion operator to a function pointer. The unary + triggers this conversion.

Initialisation in C++17 – the matrix

C++ Weekly - Ep 144 - Pure Functions in C++

int square(int value) __attribute__((pure));
[[gnu::pure]] int square2(int value);
[[gnu::const]] int square3(int value);

Exhaustive and Composable Error Handling in C++

Fabian Kosmale

TL;DR: You can emulate OCaml polymorphic sum type error handling in C++17.

Code :: Reddit

class AST;
struct SyntaxError {int line; int column;};
struct GrammarError {int line; int column; std::string explanation;};
auto parse(std::string input) -> Result<AST, SyntaxError, GrammarError>;

struct LengthError {int length;};
struct HeightError {int height;};
auto validate(AST ast) -> Result<AST, LengthError, HeightError>;

struct DisplayError {std::string explanation;};
auto display(AST ast) -> void;
auto result = parse(my_input)
  .Case<SyntaxError>([](auto err){
    report_error("Invalid syntax at line", e.line, ":", e.column);})
  .Case<GrammarError>([](auto err){
    report_error(e.explanation, "at ", e.line, ":", e.column);})
  .Case<LengthError>([](auto err){
    report_errror("illegal length: ", e.length);})
  .Case<DisplayError>([](auto err){
  | ESAC;
// Triggers static_assert as HeightError is unhandled




Elizabeth Zwicky:

The only thing more frightening than a programmer with a screwdriver or a hardware engineer with a program is a user with a pair of wire cutters and the root password.