Better Friendship : Passkey Idiom

Often when declaring a class as a friend, we give it more freedom than it actually needs i.e. it has access to all the private members, which in quite a few cases is not ideal. The Passkey Idiom is one way to mitigate this. Consider the following class: class Symbol...

3 minute read

Lightning Round

Pre-increment vs Post-increment When writing a loop, or otherwise, you can increment something by specifying either ++i or i++. While in most cases it doesn’t matter which one you use, prefer to use the pre-increment ++i operator if you can. This is because, the type that is being incremented or...

6 minute read

Choosing Your Function Arguments

A function name and it’s arguments should be enough to let a user know what the function does without browsing the function’s source code. While the name of the function plays a big part in that, the function arguments are the main communication tool that can be used to convey...

4 minute read

The Shape of Lambda

In this one we’ll look at how a lambda is represented and how we can take advantage of it. Given this lambda: auto const printString = [LineEnd=envLineEnd](std::string const& str) { OutputDebugStringA(str.c_str()); OutputDebugStringA(LineEnd.c_str()); }; Here we have declared a lambda of name printString using the auto keyword. This is a pretty...

5 minute read

Local Classes

Generally speaking it’s always good advice to keep your variables as local as possible. (Unless you have evidence against it for a particular use case). The same can be said for classes. If you have a class that is either not going to be instantiated outside a particular function /...

2 minute read

The Perils of Undefined Behaviour

There are quite a few cases where the C++ standard simply states that if certain conditions are met then the behaviour of the program is undefined. This means that there is no guarantee as to what the program will do. Compilers on the other hand love undefined behaviour (UB), especially...

7 minute read

The Rules of the 5

In this TotW, we’ll look at how the special member functions can affect code gen. But before we do that, let’s look at the semantics of how adding a special member affects other members. The following picture, taken from Howard Hinnant’s talk perfectly summarizes this: Take a minute or two...

13 minute read