Well, if he’s not sure…

June 2nd, 2014

I just stumbled across a comment by one of the most highly respected C++ library implementors there is, regarding part of the C++11 random number generation suite:

Or there is this thing called seed_seq which is a souped-up seeder on steroids. I’ve implemented the thing and am still not quite sure when to use it. :-)

Well, if he’s not sure, I’m sure I have no idea…

LLVM Clang Compiler Now C++11 Feature Complete

April 23rd, 2013

This is good news – we now have an open-source C++ compiler with support for all C++11 language features.

WG21 Meeting Notes – Bristol

April 20th, 2013

I’m currently sitting in the ISO C++ Standards Committee (WG21) meeting in Bristol, and we’ve just voted a couple of things into the C++ standard that I think are rather nice:

Binary Literals:

I can now write:

const int five = 0b0101;

Return Type Deduction for Normal Functions

This is really nice: the compiler can now deduce the return type of a function rather than my having to specify it, so instead of writing:

some_really_long_typename f()
return some_really_long_typename();

I can now write:

auto f()
return some_really_long_typename();

A little thing but it helps. The deduction, of course, works on many more complex cases that this, but I’m back to paying attention ;)

Mark Webber – time to get out of F1?

April 13th, 2013

I’m a big fan of Mark Webber. He’s a great driver, and a fabulous character. Formula One needs more drivers like him.

But over the last couple of years, we’ve seen a change in his demeanour around the paddock; he’s not enjoying racing as much as he used to. I remember his debut ten years or so ago for the Minardi team (run by that other irrepressible Aussie, Paul Stoddart). He was young, keen and competitive. He’s still competitive (at least, as far as his team allows him to be) – but it seems that racing has become a chore for him. This can’t be good for him personally.

I lay the blame firmly at the door of the Red Bull team. They’ve made it clear (by actions, if not by words) that he’s the number two driver in their team. We’ve seen this before in F1 (remember poor old Rubens Barichello trying to convince us all that he had equal status with Schumacher at Ferrari? Or Ronnie Peterson playing second fiddler to Andretti back in the 70s?) – and no doubt we’ll no see it again. But I don’t think I’ve ever seen a driver so completely demoralised by it.

One option might be to move to another team – but where? Right now, he’s in one of (if not the) quickest car on the gird; I think it’s unlikely that he could engineer a move to a team that could give him a competitive drive – and a car that struggles to get into Q2 is going to be even worse that his current situation.

If I were Mark, I’d seriously consider getting out of F1, and got and drive in a series where he can enjoy the racing again.

For me, it would be a tragedy if Mark left the sport – but I think that for him, it’s possibly the best thing he can do right now.

Earth, Night Glow, Aurora and Atmosphere.

July 16th, 2012

This video, shot from the International Space Station, is well worth taking a look at.

Home Automation : The Kira128

June 16th, 2012

I’ve been interested in home automation in various flavours for almost as long as I can remember – I guess it’s the combination of being a bit lazy, and tech for the sake of tech.

One of my favourite toys is the Kira128, from Keene Electronics which is basically a little box that sits on the end of an ethernet connection and responds to UDP packets by sending out infra-red (IR) commands to your TV/Cable/Satellite/DVD/PVR/etc. A universal remote that responds to your network.

The unit is intended to be hidden away somewhere near your A/V rack, so it’s visually a little uninspiring (a cream box, with a few LEDs, an ethernet socket and so on) – but that’s ok. I’ll probably paint mine black at some point.

The functionality, however, is very impressive for a unit at this price. Some software is provided to enable you to set it up and record codes from your existing remotes – this is probably the weakest part of the product; I’ve spent much of my career working in embedded systems development, and this “feels” like something that’s been written by an engineer to test something in the workshop, rather than an end-user tool. (Since I’ve been that engineer, I rather like the fact that it exposes all sorts of functionality, but most users aren’t going to care much about some of it – and it could be a bit easier to use). Fortunately, you don’t have to spend much time with this – just record codes from your existing remotes, and forget about it – so it’s less of a problem than it might be. I keep thinking about writing something a bit more user-friendly, but I use this so rarely that it’s a low priority.

Once it’s all set up, it includes a simple webserver which provides a perfectly workable UI that will let you drive all the functions from your smartphone/tablet/PC/etc.

Importantly (for me, anyhow), the network protocol is fully documented – which allows you to do all the configuration and control over a network without using the provided software. I wrote a very simple iOS application which will allow you to drive this unit from your iPhone (and, very probably, your iPad – although I’ve not tested tha) – you can find the code on github. (It’s not actually any improvement over the built-in web interface, but it was fun to write, and I figured out some stuff about iPhone development along the way). I’ve also written a simple webapp that runs on my NAS which gives me a nicer feeling web UI. I plan to publish that on GitHub at some point, but currently it’s horribly hard-coded to work with my particular setup, so that’ll have to wait.

A note about Keene, the manufacturer: I’ve been very impressed with their customer service. I had a problem with the first unit I received, so I fired off an email; next morning, I had a phone call from Keene, and a replacement unit arrived at my home the next day (along with a pre-paid jiffy to return the faulty one). The new unit has worked flawlessly ever since. They’ve also been very forthcoming with information about programming and configuring the units, so top marks to them.

This is a topic I’m likely to write more on – but if you’re interested in a solution to IR control over IP, I’d thoroughly recommend the Kira128.

(And yes, it wouldn’t be difficult to do something like this with a Rasberry Pi or Arduino and some external components – but that’s a column for another time…)

Choosing an STL Sequence Container

June 9th, 2012

The C++ Standard Template Library has a fairly good selection of container classes, and in particular “sequence” containers. For some reason (I’ve always assumed it’s familiarity), most people seem to use std::vector as their container-of-choice, but experience shows that it’s actually the best choice less often that one would think.

So how do you pick the right sequence container? Often, it’s not an easy or obvious choice. Each container has different trade-offs : there’s the obvious difference of some provide features that others don’t – but there are also the more subtle differences in memory allocation characteristics, performance guarantees, and some perhaps less easily quantifiable characteristics (such as cache locality).

This is based on something I wrote a little while ago when corresponding with my friend & colleague Luca Bolognese on his recent series of articles on functional programming in C++ (which is well worth a read).

Just as there’s no “one size fits all” container (and, by inference, no “one size fits all default” container), there’s no one size fits all mechanism for choosing a container (although profiling can help a lot) – but this is roughly how I choose a container (before I can profile, naturally!).

A decision tree for selecting a sequence container:

  • I’m in a rush and can’t be bothered to read the rest: use std::deque
  • I know at compile time exactly how many elements will be needed (and will they all fit on our stack!) – If so, use std::array.
  • I need constant-time random access (it’s worth noting that we often *think* we do, but actually don’t – YAGNI applies). If you DO need it, then we can eliminate std::list/std::forward_list as candidates.
  • I need bidirectional iteration (again, we need this less often that we thing). If so, eliminate std::forward_list from consideration
  • Will elements often be added/removed “in the middle” (i.e. not at the ends)? std::forward_list or std::list are probably the best choices. (Unless you eliminated them earlier due to constant time random-access requirements); these two are particularly useful when the contained elementes are expensive to move/copy, since they don’t need to shuffle things around when you add or remove elements. Of course, it may be possible to reduce the cost of move/copy by having a container of (preferably smart) pointers, rather than directly containing the objects.
  • Do we need the contents as a contiguous array? use std::vector (and call reserve with a reasonable guesstimate beforehand if we can) – sometimes, anyway; I’ve seen cases where it’s faster to build my collection as a std::deque, then transform to std::vector later on.)
  • Use std::deque.

Or, to put it another way, “default to std::deque unless there’s a good reason not to”.  That’s an overly-strong statement, but it’s a reasonable starting point. In a typical application, deque will give you better memory allocation characteristics than any of the others (aside from std::array, but that’s extremely limited in its application). vector can beat it IF you have a good estimate for how big the collection is going to get (but otherwise can tail off dramatically). deque will typically give you better cache locality than list/forward_list, and it’s insert/remove at the ends characteristics are better than any of the containers except the two list types.

Naturally, every case will be different – and no set of ‘rules of thumb’ is going to work every time (this is one of the things which distinguish rules of thumb from laws of physics). Commonly, we don’t know ahead of time which of those criteria is going to have the most significant impact on what we’re doing; when I find myself in that situation, I’ll use std::deque; once I have a firmer idea of the criteria, I’ll go back and revisit that choice (which – quite often – is as simple as changing a typedef). And of course – as with all ‘optimisations’ – measure, measure and measure again!

Unit testing – with a Catch

June 9th, 2012

For fun, I’ve been working on a little iPhone project (a simple password manager – the code is pretty horrible right now, but you can find it on github if you’re curious).

Naturally, I wanted to write some unit tests – and thought it was about time to try out a framework I’ve been meaning to take a look at for a while; it’s called Catch, and written by a friend (and sometime colleague) of mine – Phil Nash. First impressions are very good.

It’s amazingly easy to get up and running; just download it, include a header file – and write a few tests. Here’s a trivial example, with a couple of tests:


#define CATCH_CONFIG_MAIN #include "catch.hpp"
TEST_CASE( "Simple/Number1", "A simple test" )
  int i = 0;
  REQUIRE( i == 1 ); // Naturally, this should fail!
TEST_CASE( "Simple/Number2", "Another test" )
  int i = 0;
  REQUIRE( i == 0 ); // .. and this should pass

And there you go – compile that, and run it – the output looks something like this:

[Started testing]

[Running: Simple/Number1]
test.cpp:7: i == 1 failed for: 0 == 1
[Finished: 'Simple/Number1' 1 test case failed (1 assertion failed)]

[Testing completed. 1 of 2 test cases failed (1 of 2 assertions failed)]


It has some very nice features:

  • It works for both C++ and Objective-C (strictly, the test cases need to be in Objective-C++, but you test Objective-C components with them)
  • The ease of use of the TEST_CASE macro, which hides all the horrid boilerplate that many frameworks seem to insist on.
  • It works on multiple platforms (although so far I’ve only actually run it on OS X).
  • Distributed under the Boost licence, which is about as permissive as they come.
  • It’s a header-only implementation, so no libraries to build and link to.
  • It’s much easier to get yourself started than any other testing framework I’ve used (and I’ve used a few…)

So what’s the Catch? Well, none that I can see – aside from Phil reserves the right to use that particular pun well past it’s sell by date… (Phil has a truly astonishing stockpile of puns, so I’m not sure why he thinks he needs another – but there you go…)

It’s early days for me with Catch, but I’m sure I’ll write more on it some other time. But for now, it gets my recommendation.

What is it with printer manufacturers?

May 27th, 2012

I recently purchased a new printer (A Canon Pixma MG3150, if you’re curious; I’ll probably write more about that when I’ve had it a little while – but first impressions are good).

Now, since I print stuff very rarely, but needed it in a hurry, this isn’t a particularly high end unit. The purchase was based more on price and next-day availabilty than anything else. So it was cheap. That’s ok – and I’m happy with the printer itself (everything works, the print quality is farily good, etc, etc).

But that’s not why I’m writing this; I’m writing this because of one of my long-standing complaints about printers – or rather, the software that comes bundled with them.

WHY do the manufacturers seem to think it’s important to bundle huge amounts of software I’ll never use with their products – and then force me to install all of it just so I can print a page of text?

You know the kind of thing – photo album managers (if it was a camera, then maybe..), web page clippers (no, that’s what I have a web browser for). I just don’t get it.

And, of course, the quality of this software is generally pretty so-so. For the most part, it has a flashy (and, usually, a horribly non-standard and terribly UNintuitive user interface) -and for what? It doesn’t encourage me to buy their product. (I didn’t even know it was all in there until the box arrived) – and it seems to take aeons to install. Of course, the (un)installers are usually badly broken – so it scatters itself all over my machine in a most insidious way. (No doubt there will be remnants of it on my Windoze box until I reinstall the OS; I’m not letting it anywhere near my Mac).

If I want to mange my photos, I’ll use iPhoto. If I want to edit them, I’ll use The Gimp. If I want to clip webpages, I’ll use Chromium. I have plenty of good options for all of that kind of thing. What I don’t (for the most part) have options for is where to source my printer drivers.

No, I’d MUCH rather manufacturers spent the time, effort and money the spent on developing this junk on writing really high quality drivers for their products – and leave all the ancillary stuff to groups who specialise in that kind of thing.



Yes, I’m aware that the manufacturers generally don’t write all this bundled bloatware themselves – they more than likely licence it from some body shop somewhere that’s never even seen the printer. I just wonder why they do that.