Acceptable Noexcept

In this tip we’ll look at the caveats associated with noexcept. The noexcept Guarantee One common misconception is that noexcept implies that the function will not throw. This is wrong. Decorating a function with the noexcept specifier means that if the function does end up throwing, then it will result...

6 minute read

Guidelines For Initialisation

This is a combination of Herb Sutter’s GotW #1 and Abseil Tip #88. There are about 18 ways to initialise a variable in C++. Especially given C++ 11’s uniform initialisation syntax it’s getting pretty complicated. Use the following guidelines when trying to initialise a local or member variable: Use {}...

3 minute read

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