Tag Archives: cplusplus

Compile-Time Polymorphism

Let’s say we have a peripheral, like an accelerometer, connected via an SPI bus. Then let’s say we have to different embedded projects that make use of this same chip, implemented on two different microcontrollers. Wouldn’t it be nice to be able to write the accelerometer communication code once, keep a low runtime overhead that… Read More »

Lua and C++

I’ve not yet had a project that’s needed an embedded scripting language. When I do, I think I’ll reach for Lua. $ apt-get install lua5.2 liblua5.2-dev Command line lua: $ lua Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio > print("Hello, World") Hello, World Embedded lua: #include <lua5.2/lua.hpp> int main() { // Create a Lua virtual… Read More »

Fizz Buzz Testing

Unit testing is the name we give to the idea of writing code that tests our application code automatically. Essentially, its job is to exercise each piece of your application in isolation, and ensure that any data dependent bugs you might introduce accidentally, never make it into production releases. Personally, I also find it makes… Read More »

Pointer Implications (C++11)

While C++98 had smart pointers of a sort; they weren’t good enough for use. The language lacked the key feature that makes smart pointers work: move semantics. C++11 has introduced that language feature, and hence has solid smart pointers. The addition of smart pointers gives us some further options for passing references to objects when… Read More »

Pointer Implications (C++)

Last time we covered pointer arguments, and that article was applicable to C and C++. C++ introduced true pass-by-reference semantics. f( O & ) f( const O & ) f(O &) Previously we were careful to note that passing a pointer to a function was still only pass-by-value. The pointer was copied, and then the… Read More »


Last time, I spoke about lambdas, and left you with this comment about other features of lambdas. One that’s particularly worth bearing in mind is the ability to pass this as a lambda parameter. That means you can get access to more than just the enclosing scope variables, you can get access to the enclosing… Read More »

La Lambda

Let’s say we’ve loaded a database table into memory and now we want to do some work with it. I’m imagining a case where we’ve selected a small subset of records from an enormous table (i.e. it wasn’t cheap to fetch) and we want to pull records that meet particular conditions from our subset. Let’s say… Read More »


Let’s say we are reading tagged blocks from within some sort of packet. The blocks being tagged means we won’t care whether they’re present, what order they come in, and can have each tag be a different length. <tag2> <tag2_data0> <tag2_data1> … <tag2_data11> <tag1> <tag1_data0> <tag1_data1> … <tag1_data8> <tag6> <tag6_data0> <tag6_data1> … <tag6_data20> <tag3> <tag3_data0>… Read More »


Consider this: #include <iostream> using namespace std; int x = 10; int f(int& x) { x++; return x; } void g(int& x) { x *= 2; return x; } int h(int f, int g) { return f + g; } int main() { clog << "h() = " << h(f(x), g(x)) << endl; return 0;… Read More »

Don’t Take Exception

Exceptions were an enormous improvement for error handling in software. Previously we would have code like this: int a() { if( !do_something() ) return error_code_for_a; return success_for_a; } int b() { if( a() == error_code_for_a ) return error_code_for_b; return success_for_b; } int c() { if( b() == error_code_for_b ) return error_code_for_c; return success_for_c; } int… Read More »