Iterating over a vector is a pretty simple task we get to do pretty often. It can be achieved in quite a few ways:
- Using normal random access (operator with index).
- Using std::iterator.
- Using std::for_each algorithm.
I set out to check the runtime differences between all these options, and the results turned out to be a little surprising (or not).
Continue reading Different ways to iterate over a vector
When you throw an exception, the thrown object is copied to a special location in the memory. Now, suppose you wanted to catch an exception (by reference ofcourse), alter it slightly, and then rethrow it with hope that the same object would be rethrown. The language provides two ways to do just that, which are very different in what they actually do below the surface.
Continue reading Nuances of exception rethrow
Sometimes you would like to create an abstract class, but there is no method you could naturally declare as pure virtual (=0) in order to achieve that (in a tag interface, for example).
In such cases the destructor may be the perfect candidate: as it should always be declared virtual if inheritance is considered, it may as well be pure virtual.
But then we have a hazard on our hands – since destructor (and constructor) invocation in inheriting classes is recursive, we will end up with a pure virtual method call – with no suitable code to execute. How do we solve this issue?
Continue reading Pure virtual destructor
This question was written by a friend who luckily let me use it. It presents a very common pitfall of C++, regarding the syntax of declaration statements.
Continue reading Problematic declaration syntax
Once in a while so it happens that a programmer wishes to time his own creation. This article presents a simple and portable implementation of a Timer class which saves the time of its creation and computes the time it took until destruction, effectively measuring the runtime spent in the enclosing scope.
Continue reading Portable measurement of execution time
Today, a somewhat basic post.
Suppose you have a multi-dimensional array and would like to pass it to a function, but still be able to access it easily (by using row,col tuples for instance): passing the pointer is probably not the right way to go (although you could cast inside, but that’s ugly).
Continue reading Passing multi-dimensional arrays
The classes istringstream and ostringstream can be utilized to create generic templated toString() & fromString() functions. This functionality (which is built-in in many languages, such as Java) may come in very handy, quite often.
Continue reading Implementing toString() and fromString() using std::stringstream
When using placement new operator, destruction isn’t as transparent as usual..
Continue reading Destructors and placement new
Most compilers implement dynamic binding by using a vtable whose pointer resides at the beginning of each object’s memory footprint (something along the lines of [vtable-pointer|..members..], if we are not considering virtual inheritance).
Keeping this idea in mind, why don’t we go ahead and attempt to change that vtable pointer?
Continue reading Changing the vtable pointer
From time to time we are required to print out a vector. It is possible to utilize the standard copy algorithm (std::copy) to do just that.
Continue reading Printing using std::copy