C++11

For my current contract I finally have access to a C++11 compiler, and am starting to use C++11 in a production environment. Yes, I’m late to the party but that’s what happens when you have a succession of clients using older compilers.

I’m only scratching the surface so far, but things I am really liking so far are the auto keyword, initialiser lists, and the extensions to the for command to make iteration more compact.

Consider the following traditional C++ code for initialising a vector and then iterating over it:

std::vector<int> myVector;
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);
myVector.push_back(4);
myVector.push_back(5);

for (std::vector<int>::const_iterator it = myVector.begin(); it != myVector.end(); ++it)
    std::cout << *it << std::endl;

Now consider the same code written in C++11

std::vector<int> myVector = { 1, 2, 3, 4, 5 };

for (auto elem : myVector)
    std::cout << elem << std::endl;

Life just got a whole lot more convenient.

Update:

Note that for more complex types where there is a significant cost to copy, you would probably want to use the following instead:

for (const auto& elem : myVector)

Hello, and welcome

Hi there. Welcome to my personal blog.

I intend this blog to be a general dumping ground for information that I consider to be useful and might want to refer back to. If other people wind up here due to a search engine and find that one of my posts is useful to them too then so much the better.

Due to my various interests, there will probably be a fairly eclectic mix of stuff to do with cars, C++ programming, music, humour, and whatever else takes my fancy.

The blog will be in a state of flux for a few days as I work out the look and feel of it, so please bear with me.