Recursive lambdas

One can assign a lambda to auto or to std::function. Normally one would assign a lambda to auto to avoid possible unwanted allocation from std::function. But if you want recursion, you need to be able to refer to the lambda variable inside the lambda, and you can’t do that if it’s assigned to auto. So… Continue reading Recursive lambdas

Rules for using <random>

These days, it’s easy to do the right thing. Don’t do this: Don’t use std::rand(). Ever. Don’t use std::random_shuffle() to permute containers. (Too easy to misuse; can use std::rand() under the hood.) Don’t use any kind of clock for a seed. Don’t use mod (%) to get a random value into a range. It introduces… Continue reading Rules for using <random>

C++ Tuples: the missing functionality

C++ provides a strange mix of compile-time and runtime functionality for dealing with tuples. There are some interesting parts, like std::tie to destructure a tuple, and std::tuple_cat to join together several tuples into one. So there is evidence that the standard has been influenced by some functional programming ideas, but I don’t think the full… Continue reading C++ Tuples: the missing functionality