C++: variant

Let's suppose I have a system that handles students, teachers and crew of a school. To model that in an object oriented style, I would have a class hierarchy similar to this one: And my collection would be defined like this: where the size_t ID would be the key of the map. Since I do… Continue reading C++: variant

Advertisements

C++: “auto” return type deduction

Before C++14, when implementing a function template you did not know the return type of your functions, you had to do something like this: You had to use "decltype" in order to say the compiler: "The return type of this method is the return type of method do_something of object a". The "auto" keyword used… Continue reading C++: “auto” return type deduction

C++: “auto” on anonymous functions

C++14 introduced an improvement to the way we can declare anonymous functions (a.k.a. lambda expressions). For example, before C++14, if you wanted to pass an anonymous function to the sort algorithm, you had to do something like this: Since C++14, you can let the compiler to deduce the type of the variables passed as parameters… Continue reading C++: “auto” on anonymous functions

classeine: Introducing classeine

Hello, The last three months in my spare time I was developing a framework (called classeine) aimed to make easier to code and run server-side RESTful apps using modern C++. My first preview (actually more a proof of concept than a proper release) has these features: Include files only, totally based on templates. No virtual… Continue reading classeine: Introducing classeine

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 the tuple; for example: tuple<int, int> x; will store 2 integers. tuple<string, int, bool> y; will store one string, one integer… Continue reading C++11: std::tuple

C++: Smart pointers, part 5: weak_ptr

This is the last of several posts I wrote related to smart pointers: Smart pointers unique_ptr More on unique_ptr shared_ptr weak_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 administration in a deterministic way so you will not… Continue reading C++: Smart pointers, part 5: weak_ptr

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 Hello world The problem with my implementation is that it only works with arguments passed as constant references, so if I would like to invoke… Continue reading C++11: Perfect forwarding

C++: Smart pointers, part 4: shared_ptr

This is the fourth post of several posts I wrote related to smart pointers: Smart pointers unique_ptr More on unique_ptr shared_ptr weak_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… Continue reading C++: Smart pointers, part 4: shared_ptr

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

This is the third post of several posts I wrote related to smart pointers: Smart pointers unique_ptr More on unique_ptr shared_ptr weak_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… Continue reading C++: Smart pointers, part 3: More on unique_ptr

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

This is the second post of several posts I wrote related to smart pointers: Smart pointers unique_ptr More on unique_ptr shared_ptr weak_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… Continue reading C++11: Smart Pointers, part 2: unique_ptr

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 method does not wait for the result of the execution and continue doing what is next; in this way, the… Continue reading C++11: std::future and std::async

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 as parameters-by-value and the "result" variable is copied before being passed to the bound function add. Why? Because bind… Continue reading C++11: reference_wrapper

C++: SFINAE

Say you have this piece of code: If you compile the code you will get an error similar to this one: vec.cpp: In function 'int main()': vec.cpp:22:18: error: no matching function for call to 'show(int, int)' vec.cpp:22:18: note: candidate is: vec.cpp:7:57: note: template void show(typename T::iterator, typename T::iterator) And the error is quite obvious: your… Continue reading C++: SFINAE

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 to a simple function, to a functor or to a lambda expression. For example, if you want to store several functions, functors or lambda… Continue reading C++11: std::function and std::bind

C++: Smart pointers, part 1

This is the first of several posts I wrote related to smart pointers: Smart pointers unique_ptr More on unique_ptr shared_ptr weak_ptr Memory management in C is too error prone because keeping track of each bunch of bytes allocated and deallocated can be really confusing and stressing. Although C++ has the same manual memory management than… Continue reading C++: Smart pointers, part 1