Category Archives: C/C++

Discriminated Unions

In computer science, a discriminated union is one of the many names given to the concept of a “catch-all” datatype. (You’ll also hear it referred to as a variant.) It’s meant to hold data of any type at any given … Continue reading

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

Inline Namespaces

One of the neat, new language features of C++0x that is targeted firmly at framework designers is the ability to declare “inline” namespaces. While the name may seem a bit strange at first, the concept is quite intuitive. It allows … Continue reading

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

Warning: Don’t Ignore Warnings!

How many times have you run across code that looks fine, works fine, but still generates a message like “Signed/unsigned mismatch” when we compile it? How many times have you thought to yourself, “that’s stupid, I know this code is … Continue reading

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

Move Semantics

One of the new features in C++0x is a way to express move semantics. This is a sensible piece of sibling functionality to copy semantics, which you’ve likely already run into. When writing copy semantics for a class, the idea … Continue reading

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

The Things You Spot During Code Reviews

I was doing an informal code review the other day, and I ran into some code that I thought would make a fascinating blog post. It had to do with a pure virtual destructor declaration, followed by the same destructor’s … Continue reading

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

Hiding in Plain Sight

Sometimes, the hardest bugs to find are the ones that hide in plain sight. They’re the sort of bug where your eyes skim over the offending code and your brain continuously says “yup, fine, right, good, yup” and you can’t … Continue reading

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

Function Pointer Basics

At their core, functions are nothing more than a location in memory with some special semantics attached to them. The assumption is that the location in memory is the start of some machine code to be executed, and that there … Continue reading

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

Allocations and Exceptions

One of the things I dislike about many programming languages are exceptions. They go against the natural flow of thinking for most programmers. We tend to think of code as flowing in one direction only: forward. But with exceptions, code … Continue reading

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

When Should You Use const_cast?

C++ provides an explicit casting mechanism called const_cast, and yet the question pops up: when would I ever use this? You can always assign a non-const value to a const value without requiring a cast operation. So you don’t need … Continue reading

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

When Const isn’t Const, it’s Mutable

I’m a programmer who strives to write const-correct code. I’ll admit that it can be challenging, but once you get used to doing it, your code is generally cleaner and more maintainable. This happens at the expense of flexibility. When … Continue reading

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