c++

std::vector and C++'s braced initialization

The new versions of C++ (starting from C++11) include a unified initialization syntax based on braces. In most of the cases it offers a more elegant and unified way of initializing objects. Examples of its usage are as follows: std::string name{"Michael Scott"}; MyClass obj{}; // default constructor Braced initialization can also be used in constructors to initialize class members: MyClass::MyClass(const Something& value) : member{value} { // Additional operations ... } When it comes to be widely-used std::vector, braced initialization might be a source of confusion.

Searching for the closest value in a sorted vector with C++

In this post I am going to show how to efficiently search for the closest element in sorted std::vector using the std::lower_bound function from the Standard Template Library (STL). The problem is a follows: you have a bunch of numbers (e.g. doubles) that you have stored in a std::vector, which you have subsequently sorted, and you want to, given a new number x, find the element in the sorted vector that is the closest to x.

Building C++ code from command line on Windows

After many years of working exclusively in Unix environments, Windows experience appears daunting, in particular when it comes doing stuff in terminal. I have heard a lot of about the power of PowerShell, but I have never had enough motivation to explore it — I sticked to the familar Bash on Mac and Linux for most of my work. However, I was always interested in mastering the task of building C++ code using the native compiler on Windows (Visual C++) without using any GUI tools.

Position independent code

In this blog post, I am going to provide a short overview of the concept of position independent code and its specification in CMake, particularly in projects using pybind11. Some time ago, when I was working on the FxIS project, which was developed in C++ with the additional Python extension realized with pybind11, I encountered a linking error, which included a line similar to this: /usr/bin/ld: libcore.a(sum.cpp.o): relocation R_X86_64_PC32 against symbol .

C++ move semantics

In the modern versions of C++ (starting from C++11) one encounters a concept of move semantics. It may confusing a bit, and in this post I am going to provide my notes on the topic (mostly based on Bjarne Stroustrup’s The C++ Programming Language (4th edition): sections 3.3, 7.7.2, 17.5.2). The first important concept is rvalue reference. For a type T, a rvalue reference is T&& (note the double ampresand). It basically represents a reference to a temporary object that is assumed to be modified and not used again.

Exposing C++ array data in Python with the Pybind11 buffer protocol

Python works great together with native code. The success of the Python data science stack is largely attributed to the capability of building native extensions. It is no secret, however, that the out-of-the-box C-based facilities are rather cumbersome to use. Luckily, there are a bunch of alternatives out there. The one I liked the most is Pybind11, a fork of Boost.Python with focus on the modern C++ standards. It is distributed as a lightweight header-only library that can be bundled with your project.