Quantcast
Channel: Cpp11 – On C++ and other OOPscenities
Browsing latest articles
Browse All 23 View Live

Image may be NSFW.
Clik here to view.

C++11: Initializer lists

If you want to declare and initialize an array of five integers, you could do something like: But in C++03, what would happen if you would want to do the same thing using some STL container, like a...

View Article


Image may be NSFW.
Clik here to view.

C++11: Lambda expressions

Consider you have a Person class: You want to store several instances of this class in a vector: If you want to sort this vector by the person ID, you could implement a PersonComparator and use the STL...

View Article


Image may be NSFW.
Clik here to view.

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...

View Article

Image may be NSFW.
Clik here to view.

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...

View Article

Image may be NSFW.
Clik here to view.

C++11: nullptr

In C and C++ we use the preprocessor macro NULL to say a pointer is not pointing to anywhere right now. The problem with NULL is that underneath it is just a plain 0. Consider the problem looking at...

View Article


Image may be NSFW.
Clik here to view.

C++11: Variadic templates (functions)

A very interesting C++0x feature are the “variadic templates” that, in short, are template classes and functions with a variable number of parameterized types. They can be useful to do several things,...

View Article

Image may be NSFW.
Clik here to view.

C++11: C++-style listener list

Consider you want to create a class where you can register a set of listeners to be notified when something occurs. For example, this is a class that performs some task and you want to be notified when...

View Article

Image may be NSFW.
Clik here to view.

C++11: unordered maps

The STL ships with a sorted map template class that is commonly implemented as a balanced binary tree. The good thing on this is the fast search average time ( O(log2N) if implemented as a balanced...

View Article


Image may be NSFW.
Clik here to view.

C++11: std::function and std::bind

std::function and std::bind were born inside the Boost C++ Library, but they were incorporated into the new C++11 standard. std::function is a STL template class that provides a very convenient wrapper...

View Article


Image may be NSFW.
Clik here to view.

C++11: enable_if

std::enable_if is another feature taken from the Boost C++ library that now ships with every C++11 compliant compiler. As its name says, the template struct enable_if, enables a function only if a...

View Article

Image may be NSFW.
Clik here to view.

C++11: reference_wrapper

Look at this piece of code: This program supposedly adds 80 to 20 and prints the result; it compiles perfectly but when you execute it; you get…. 0! Why? Because the bind method receives its parameters...

View Article

Image may be NSFW.
Clik here to view.

C++11: std::thread

The standard library that ships with the new C++11 contains a set of classes to use threads. Before this, we needed to use the OS specific thread facilities each OS provides making our programs hard to...

View Article

Image may be NSFW.
Clik here to view.

C++11: std::future and std::async

C++11 introduces support for asynchronous calls in a very easy way. An asynchronous call is a method invocation that will be executed in a separate thread (or core or processor); so, the caller of the...

View Article


Image may be NSFW.
Clik here to view.

C++11: Smart Pointers, part 2: unique_ptr

C++11 ships with a set of out-of-the-box smart pointers that help us to manage the memory easily. One of those smart pointers is the unique_ptr. Consider this piece of code: If you execute this code...

View Article

Image may be NSFW.
Clik here to view.

C++: Smart pointers, part 3: More on unique_ptr

Ok, here I am going to write about two other features that unique_ptr has that I did not mention in my last post. unique_ptr default behavior consists on take ownership of a pointer created with new...

View Article


Image may be NSFW.
Clik here to view.

C++: Smart pointers, part 4: shared_ptr

As I mentioned in other posts, C++11 brings a new set of smart pointers into C++. The most useful smart pointer is shared_ptr: Its memory management policy consists in counting the number of shared_ptr...

View Article

Image may be NSFW.
Clik here to view.

C++11: Perfect forwarding

Consider this function template invoke that invokes the function/functor/lambda expression passed as argument passing it the two extra arguments given: Nice, it works as expected and the result is: 30...

View Article


Image may be NSFW.
Clik here to view.

C++: Smart pointers, part 5: weak_ptr

Some posts ago, I wrote about shared_ptr. In modern C++ applications (C++11 and later), you can replace almost all your naked pointers to shared_ptr and unique_ptr in order to have automatic resource...

View Article

Image may be NSFW.
Clik here to view.

C++11: std::tuple

A tuple is a C++11 construction and it is built heavily on variadic templates. A tuple is a variadic class template that stores an unlimited set of values of different types, defined when instantiating...

View Article

C++11: ‘decltype’

decltype is a new keyword that infers the type of a given expression. Though it can be used instead of auto; it is used mainly on return types. For example; this function: could be written using...

View Article
Browsing latest articles
Browse All 23 View Live