Enums in C++

Enumerations are useful when we want to have a custom type, that can only have a limited number of values. For example:

1
2
3
4
5
enum Mode {
  EASY,
  MEDIUM,
  HARD
};

We can create variables of type enum that can only have one of the previously defined values:

1
Mode userMode = EASY;
Read More

C-style arrays vs std::array

In this post I’m going to talk about the advantages of using std::array vs a c-style array whenever it is possible to do so.

C-style arrays

Let’s start by remembering what a c-style array is.

In C and C++ we can create an array of ints like this:

1
int arr[3];
Read More

Creating a C++ package with Conan

In the past I wrote an article that explains how to consume packages using Conan. In this article I’m going to explain how we can create our own packages with Conan.

I’m going to create a simple cpp library and make a conan package out of it.

Sample package

The first step is to create a folder for the library:

1
2
mkdir MyLib
cd MyLib
Read More

Variadic templates / functions in C++

A variadic function is a function that can take a variable number of arguments.

I have used variadic functions in previous posts, but I have not explained how they work. This post will cover that.

C-style variadic functions

C++ supports the C-like syntax for writing variadic fuctions.

Let’s say we want to write a function that adds numbers together. If we wanted to write a function that adds two numbers, we could do it easily:

1
2
3
int add(int a, int b) {
  return a + b;
}
Read More

Passing functions as arguments in C++

Using functions that take functions

There are times, when it is convenient to have a function receive a function as an argument. This technique can be seen in the standard C++ library. As an example, std::transform can be used to create a series of values based on other values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector>
#include <algorithm>
#include <iostream>

int makeNumbersBig(int small) {
  return small * 10;
}

int main() {
  std::vector<int> numbers{1, 2, 3}; // numbers has 3 values: 1, 2, 3
  std::vector<int> bigNumbers(3); // bigNumbers has 3 values, default
                                  // initialized: 0, 0, 0

  // Starting at numbers.begin() and until numbers.end(), execute
  // makeNumbersBig and store the result in bigNumbers
  std::transform(
    numbers.begin(),
    numbers.end(),
    bigNumbers.begin(),
    makeNumbersBig
  );

  // Print the values of bigNumbers
  for (const auto big : bigNumbers) {
    std::cout << big << std::endl;
  }
}
Read More

C++ Generics / Templates

A while ago I discovered generics in Java. Today, I’m going to explore how to do the same with C++.

Generics in C++ are known as templates. We use the keyword template to tell the compiler that we are about to define one:

1
2
template <typename T>
class Hello {};

In the example above, you can also see that typename is used to define the type. You might also see the keyword class used interchangeably (There are some scenarios where they are not interchangeable, but I’m not going to cover those in this article):

1
2
template <class T>
class Hello {};
Read More

Using thread pools in C++

In previous articles, I explored how to write concurrent code using threads, how to communicate between threads using futures, async, packaged tasks and promises, how to avoid race conditions using mutexes, and other related topics. This article is going to go a little higher level and explore some things to consider when designing a concurrent system.

Why use concurrency?

Before I started working with C++, I used to write software in PHP, JavaScript, Ruby, etc. When I wrote software on those languages, I didn’t need to worry about concurrency, why do I need to worry now?

When I was writing web applications in PHP, I didn’t have to worry about threads or mutexes. I received a request, maybe got some data from a database, did some calculations and returned a result. Why can’t life continue to be that easy?

Read More

Building a simple server with C++

In this article, I’m going to explain how to create a very simple server with C++. The server will receive a single message, send a response and then quit. For network programming in C++, we need to use some low level C functions that translate directly to syscalls.

Let’s explore the syscalls we’ll need.

socket

We’ll use the socket function to create a socket. A socket can be seeen as a file descriptor that can be used for communication.

This is the signature of the function:

1
int socket(int domain, int type, int protocol);
Read More

C++ Atomic types

An operation on data is said to be atomic if it is impossible to find the operation half-way done. It’s very easy to see when an operation is not atomic when used in a struct:

1
2
3
4
5
6
7
8
9
10
11
struct Type {
  int a;
  int b;
}

Type data;

void fillType(int a, int b) {
  data.a = a;
  data.b = b;
}
Read More