C++0x

A small circular linked list class in C++0x

Since I have need of one, I’ll go through how to write a simple circular list class that uses a constant amount of space and supports fast constant-time insertions. Insertions will overwrite the oldest entries when space runs out. Deleting from the front of the list is supported. Only forward iterators are supported and begin() and end() functions are provided. The size of the class is equal to the space needed to store the given number of elements plus the size of two integers.

read more

C++0x

---

Simplifying Lua calls with C++0x variadic templates

Lua and C++ can interact through a stack with function parameters pushed onto the stack in the same way as is done in assembly. The stack interface is very effective but it can get annoying to keep pushing multiple arguments onto the stack to call functions with multiple parameters. This style of code can also be error prone – since most of the code is repetetive a programmer will start to copy and paste code and may forget to change a lua_pushboolean call to a lua_pushnumber call, which could lead to strange bugs. However, with the variadic templates added in the C++0x standard we can easily write a few functions around the lua calls that remedy this situation.

read more

C++0x, Lua

---

Selection Sort

Since almost everyone has sorted something at one point in his or her life, sorting algorithms are, for the most part, easy to understand, which makes them ideal as a starting point in learning algorithms. Now then, let’s begin with generating something to sort. We’ll make an int vector with 10 elements (a small enough number that we can keep track of it) and we’ll fill it with random numbers:

read more

Algorithms, C++0x, Winlab Programming Talks

---

Generic Programming with C++0x

When we write code we also write bugs, so the less code we write the fewer bugs we write. One way to write less code is by writing generic code. For instance, a generic sorting function will sort many different kinds of things in a generic way. In C++ there are many types and containers that can be operated upon in generic ways so a generic sorting function can sort a list of strings, a vector of integers, or an array of floats.

Several things must be supported by a language to do generic programming. The first is that a programmer must be able to write code without giving a specific type for some of the variables. Of course in dynamically typed languages this is always the case, but in a statically typed language like C++ the language needs to support dynamic types in some way. The mechanism to do this in C++ is a template1.

read more

C++0x, Winlab Programming Talks

---