Decimal places in a floating point number

Floating point numbers are a mainstay of programming in areas such as games, graphics, and simulation. On the whole, they are easy and intuitive to use. However, they have certain quirks and issues to be aware of. For example, their representation is inherently flexible and often approximate. This means there’s no definitive answer to the question of how many decimal places they can hold.

By looking at the way floating point numbers are stored, it’s possible to understand why this happens, and what precision is likely to be available. Hypothetically, it’s possible under certain circumstances to get up to about 45 decimal places in a C++ float, and 324 in a double. However, as we’ll see in this post, it depends on context.
Read more Decimal places in a floating point number

How to check that a function is constexpr

I’ve been implementing quite a few simple C++11 constexpr functions in my ail (Avid Insight Library) project. While writing unit tests alongside it, I quickly realised that it would be very helpful to have code which verifies that a constexpr function is actually being evaluated at compile-time. It turns out that it’s remarkably easy to do.
Read more How to check that a function is constexpr

C++ constexpr functions

I’ve been very excited to see that Visual Studio 2015 supports the constexpr keyword in C++. It was introduced in the C++11 standard, and is being taken further in upcoming revisions.

There are a number of uses for the keyword, but the one which excites me the most is using it for writing functions which can be executed during compilation, potentially saving a lot of runtime overhead. In this post, I’ll show a couple of quick examples.
Read more C++ constexpr functions

Using C++ templates for size-based type selection

The standardisation of size-specific integer types in C/C++ is extremely useful for portability. That is, when you use types like uint16_t and int32_t, you know exactly what size of data type you’re getting (assuming your compiler supports it, which is usually the case unless you’re working with very specialist/embedded systems). This isn’t the case with the more traditional types like short and int whose sizes can vary from one compiler to another. However, the size is obviously part of the type name, meaning you need to alter declarations directly if you want to use a type of a different size.

Using the std::conditional template from C++11 lets you change this. Instead, you can have a template parameter which specifies the size you want (e.g. in bytes). The corresponding type declaration can be automatically deduced from there.
Read more Using C++ templates for size-based type selection

Pure virtual (abstract) final functions in C++

Today I ran across an interesting little quirk of C++11. You can declare a member function which is pure virtual, has no implementation, and which is final. That means the class can never be instantiated or inherited, and the function will never have a body.

For example:

class Widget
{
    virtual void foo() final = 0;
};

Read more Pure virtual (abstract) final functions in C++

Order of parameter evaluation in C++

The low-level details of how data gets passed into a function are often overlooked by programmers. We obviously care about passing by value vs. reference, and perhaps also by copy vs. move, but it’s easy to ignore anything deeper than that.

With C++ in particular, this can cause an unexpected problem regarding the order in which things actually happen. In this post, we’ll look at what can go wrong, and how to deal with it.
Read more Order of parameter evaluation in C++

MFC prevents bad_alloc from being thrown

According to the C++ standard, the new operator should throw std::bad_alloc if it fails. This will typically happen if your process has run out of memory. However, this isn’t the case if your program uses the (rather outdated) Microsoft Foundation Classes. In this post, we’ll look at what’s going on, and what you can do about it.
Read more MFC prevents bad_alloc from being thrown

Convert a number to a binary string (and back) in C++

Sometimes it’s useful to output the binary representation of a number in text, i.e. as an ASCII string of 0’s and 1’s. There are also situations where you might want convert back the other way, e.g. if you want to let a user enter a binary string manually. The bitset class in C++ makes this surprisingly quick and easy.
Read more Convert a number to a binary string (and back) in C++

Automatically output the callstack on a breakpoint in Visual Studio

When you’re dealing with a large program and multiple developers, it’s not always obvious how and when certain things get executed. One of the very useful ways to debug unexpected behaviour is to set a breakpoint on a suspect line of code, and examine the callstack when it gets hit to see the execution path.

For infrequent events, it’s not always desirable to halt the entire program while you do that though. Instead, you can tell Visual Studio to write the callstack to the output when the breakpoint gets hit, and immediately continue execution.

Read more Automatically output the callstack on a breakpoint in Visual Studio

Understanding C++11 move semantics

If you’re a programmer, you’ll be familiar with the difference between making a copy of an object, and making a reference (or a pointer) to it. If implemented correctly, the former duplicates the data, resulting in two (or more) independent instances. The latter allows the same original data to be accessed in two (or more) different ways. These concepts are common to many languages, and are essential to passing and returning data in your program.

As of C++11, you can think of ‘move’ as being a third alternative. It’s expanded the core features of the language, adding move constructors and move-assign operators to your class design arsenal. It’s required the introduction of a new type of reference as well, known as “R-value references”, which we’ll look at later.

To quote from Scott Meyers’ excellent C++11 training materials, “move is an optimization of copy”. In practical terms it’s very similar to copying, but can offer a number of advantages, especially where copying and referencing are impossible or undesirable. In this post, I’ll try to cover the basics of moves, so you can understand what they are and how you can start using them yourself.
Read more Understanding C++11 move semantics