Whining about iterators

In the STL, there are multiple classes of iterators: random access, bidirectional, forward, input, and output. I’d like to discuss the different types of iterators in a bit more detail, so that I can whine about “missing” functionality.
Continue reading

Posted in C/C++ | Tagged , | Leave a comment

Stupid Compiler Tricks

A coworker approached me today with an interesting problem and I figured I’d talk about the crazy acrobatics that solved it.

He wanted to fill out a list of operations to perform that could be registered easily “at compile time”, so that the list could be traversed later to perform these operations. Basically, he wanted his code to look something like this:

typedef std::map< const char *, void (*)( void ) > WorkItemList;
static WorkItemList sWork;

void some_function( void ) {
  // Do some work
REGISTER( "operation 1", some_function );
REGISTER( "operation 2", some_function );

void some_other_function( void ) {
  // Do some work
REGISTER( "operation 3", some_other_function );
REGISTER( "operation 4", some_other_function );
REGISTER( "operation 5", some_other_function );

// Later
void DoWork( const char *workItem ) {
  WorkItemList::iterator iter = sWork.find( workItem );
  if (iter != sWork.end())

So how do you make this work?
Continue reading

Posted in C/C++ | Tagged , , | 6 Comments

String Resources

On Windows, when you need to access a string resource, you turn to the LoadString API. It takes care of finding the string for you, loading it, and copying it into the buffer you supply. However, there are times when LoadString simply falls short. For instance, for my day job, I found myself needing to access localized resources regardless of the user’s current UI locale. The only way to do this is to use the FindResourceEx function and pass in the specific language you’re after. This works fine for most resources, but strings would always come back as not found! I want to cover this particular case in more depth today.
Continue reading

Posted in Win32 | Tagged , | 2 Comments

The Amazing Visitor Pattern

I’m a big proponent of using design patterns whenever they are the proper tool for the job. One of the design patterns I find myself pulling out of the toolbox fairly frequently these days is the visitor design pattern. However, it’s not one of the more common patterns you see many people blogging about. So I’d like to cover it with some real-world examples of how I put it to use.
Continue reading

Posted in C/C++ | Tagged , | 1 Comment

A simple introduction to type traits

Type traits are a slightly more advanced topic in C++ because it they are heavily used in template metaprogramming. However, it is not an impenetrable concept, and it comes with some great benefits if you like to write generic, reusable code. I’d like to give you a simple introduction to the concept of type traits, and show some of the more beneficial use cases for them.
Continue reading

Posted in C/C++ | Tagged , , | 9 Comments

An Almost Useful Language Extension

While fiddling around a bit with clang, I came across an interesting C++ language extension from Microsoft. If you’ve done library development on Windows, you’ve likely come across the __declspec keyword for things like importing and exporting symbols from a framework. You use __declspec( dllexport ) or __declspec( dllimport ), respectively. The __declspec keyword is basically Microsoft’s version of attributes, though it predates the concept (in C++) by many years. The __declspec that caught my eye was __declspec( property ). It’s way to have automatically generated getter and setter behavior for C++ class member variables. At first, I thought this was a great feature! Then I looked into it a bit more.
Continue reading

Posted in C/C++, Win32 | Tagged , , | 2 Comments


Destructors are one of those inescapable concepts in C++. We’ve all used them, many times without even really thinking about it. But how do destructors work? What can and can’t you do with destructors? There’s a lot more complexity to this seemingly simple concept than meets the eye.
Continue reading

Posted in C/C++ | Tagged , | 7 Comments

Switching on Expressions

The switch statement is a powerful construct in C that allows you to clearly express complex if/else if/else blocks as a single statement. By providing this construct, you can write code that’s easier to understand and maintain. The downside to this is that you must use constant values for the case statements, and these constants must sensibly compare using the == operator. So, for instance, it is possible to have integers in your case statements, but not possible to have strings. Attempting to compare strings with == will not work reliably due to pointer comparisons. However, there is a solution that has the power and clarity of a switch statement in C++, and thanks to lambda functions in C++11, it can be elegant as well.
Continue reading

Posted in C/C++ | Tagged , , | 1 Comment

The Plan and Random Points

For about six months, I managed to stick to a bi-weekly update schedule of Mondays and Fridays. However, I’ve exhausted my entire backlog of topics, as well as my todo list for things to write about. Instead of trying to force myself to write about something random purely due to an arbitrary schedule, I’m scaling back. Now I’m going to post whenever I have something of value to contribute to the world.
Continue reading

Posted in Uncategorized | Tagged , , | 1 Comment

Virtual Inheritance

A question came up on LinkedIn in the C++ group relating to how virtual class inheritance actually works. Since LinkedIn limits the amount of space for responses, and also manages to screw up code formatting, I decided to tackle the answer here. Before continuing, I suggest you familiarize yourself with the thread in question.
Continue reading

Posted in C/C++ | Tagged , , | 4 Comments