C++11: Range-based for loop

In C++03, if I have a vector and want to show its elements, I could do something like this: That method will be useful to show any collection that has a begin() and an end() and an iterator with an operator++(int), an operator!=() and an operator*() properly implemented. C++0x provides us a very useful range-based… Continue reading C++11: Range-based for loop

Advertisements

C++11: Move semantics

Consider this example: You have a class A and a container called List<T>. As shown in the code below, the container is just a wrapper of a std::vector. You also have a function called getNObjects that returns a list containing N instances of the class A. If you run this code, you will see an… Continue reading C++11: Move semantics

C++: The curiously recurring template pattern

Let's consider the following class: This class will be useful as the base class for implementing several specialized writers (a file writer and a console writer come to my mind right now), for instance: Nice, isn't it? We can use the Writer::write() method to write on any kind of media depending on the subclass we… Continue reading C++: The curiously recurring template pattern

C++: Inheritance on template specialization

When you implement something like: You are saying the C++ compiler: "Please, use the first template for all the MyClass<T> instances with any T except for the MyClass<T> instances where T = char". The problem I see with this approach is that you must provide full implementations for the template and for the <char> specialization… Continue reading C++: Inheritance on template specialization