Meeting 30 May 2019

What was your latest discovery about C++?

https://www.reddit.com/r/cpp/comments/blu0a4/what_was_your_latest_discovery_about_c/

URLs in code are legal

void fn() {
    https://www.google.com
    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

http://timur.audio/initialisation-in-c17-the-matrix

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

https://youtu.be/8ZxGABHcu40

https://www.reddit.com/r/cpp/comments/a2qzsv/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)
  .then(validate)
  .then(display);
Switch(result)
  .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){
    report_error(e.explanation);})
  | ESAC;
// Triggers static_assert as HeightError is unhandled

Twitter

/img/js-programmers.png

Quote

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.