Output file encoding in Python 3

Unicode is becoming very widespread now (for good reason), and one of the great benefits of Python 3.x is that it handles Unicode natively. There are different ways to represent Unicode though, so how do you set the file encoding in Python when you’re writing out to file?

It’s not immediately obvious from some of the official documentation, but thankfully it’s not complicated. I’ll give a quick overview in this post. Read more Output file encoding in Python 3

Generate UTF-8 dictionaries using gettext

I’ve been setting up localisation for an application using a combination of the Boost Locale library and the widely used gettext tools. I’m wanting to work entirely in UTF-8 because that should be suitable for pretty much any language we’re likely to need at this point, hopefully avoiding problems of mixing encodings. However, I found that the tools kept falling-back to outdated character sets instead.

Fortunately, there’s a fairly easy solution which I’ll outline quickly in this post. Read more Generate UTF-8 dictionaries using gettext

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

Emit audible warning from a batch script

Windows Batch (*.bat) scripting is archaic and painful, but occasionally useful for quick bits of environment setup. It’s not always obvious to the user when something has gone wrong though as it’s easy to lose the information amidst other text which scrolls by. An audible warning can be a useful alternative to draw attention to a problem.
Read more Emit audible warning from a batch script

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

Remote control bug on first level of LEGO Batman

I bought “LEGO Batman: The Videogame” for PC today (it was going cheap on Steam!). In the first level, I got stuck for a while because of an odd bug. You’re supposed to remote control a little green car, but for some reason it wouldn’t always move.

In summary, the fix which worked for me was to enable Vertical Sync in the game’s graphics settings. More details below.

LEGO Batman screenshot

Read more Remote control bug on first level of LEGO Batman

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++