Function alias vs Function pointer alias

Last weekend (4/4/2021) I was sitting down and working on my San Diego C++ agenda for the 25th upcoming session. One of things I really like presenting is few riddles from cppquiz.org I stumbled upon this one: https://cppquiz.org/quiz/question/227 . Simple and easy one. Here is the code in question: And the question is what is […]

std::deep_ptr

std::deep_ptr aka deep copying smart pointer has not yet been introduced to the STL though several C++ experts have proposed an implementation in the past. See n3339.pdf for one such proposal and a list of possible implementations. In this post I want to share my implementation of deep copying pointer I recently came up with. […]

San Diego C++ Meetup

San Diego C++ Meetup 25th meeting featuring JFrog and Conan! Agenda Welcome slides – goals we would like to achieve in San Diego C++ MeetupThis meeting – Hosting JFrog: Conan Package Manager for C++ in Practice If time permits:* Shout Out to a great C++ blog – “Vorbrodt’s C++ Blog”* 3 cppquiz questions* 2 years […]

Fun with TCP sockets

The purpose of this post is not to teach you about posix sockets; there’s plenty of that already. Instead I want to share two simple classes I implemented last night which encapsulate two types of tcp sockets: listening server socket and a client socket. These sockets as well as my recent serializer code will be […]

Better serializer

In my last post I described a light-weight serialization framework I recently implemented (video where I go over it in detail). After receiving great feedback from r/cpp I have implemented several improvements that were suggested to me: better unpack transform signature, safer unpacking code, unpack casting iterator just to name a few. The latest code […]

Light-weight serialization framework

UPDATE: Better serializer In my very first YouTube Channel video I discuss in detail the design of a serialization framework I came up with in response to a question I was recently asked. In this post I want to give a brief overview of how it works and how to get started using it. The […]

The future of C++

I hope the title of this post got your attention! What I really mean is the future direction of this blog… In 2019 I created 85 short articles, in 2020 I blogged only twice. The last 12 months have pulled my attention away from this site and toward mentoring other engineers at work by hosting […]

Make std::vector of T, efficiently

What I want to briefly talk about today is likely obvious to seasoned C++ programmers, but may be of benefit for the newcomers to the language and the STL: that is how to efficiently create std::vector of any number of elements of type T , whatever T may be. The code is short and sweet, […]

Singleton Pattern

First things first: if you’re still reading this blog, thanks! I haven’t posted anything since last December; it has been a rough first half of the year: COVID-19, working from home, isolation, you know the deal, but I have not given up on blogging, just finding the time for it has been nearly impossible. I […]

std::unique_ptr, or how to express explicit ownership

When we talk about std::unique_ptr we must mention the idea of explicit resource ownership as well as the concept of a resource source and sink. By wrapping a pointer inside std::unique_ptr we state that whoever holds the std::unique_ptr owns the resource explicitly: has complete control over its lifetime. Prior to C++11 this was expressed using […]

When exactly does the std::shared_ptr take ownership?

In a post I wrote few days ago titled “A word on std::shared_ptr” I was mistakenly arguing that, should the std::shared_ptr fail to allocate its control block (where the reference count and other information is stored), the passed raw pointer would not be deleted. Example: auto p = std::shared_ptr<T>(new T); Here, if the allocation and […]

In-depth look at C++ shared pointers

First, you should use std::make_shared (most of the time) to create shared pointers. It is optimized to perform only one allocation for both the reference count / control block and the object it holds. It is also safer to use std::make_shared in the presence of exceptions: some_function(std::shared_ptr<T>(new T), std::shared_ptr<T>(new T)); Prior to C++17, one of […]

Avoiding deadlocks the C++ way

When interviewing engineers for a C++ programming position the question of deadlocks often comes up (ask me how I know this 😉 ). What is it? And how to avoid it? Often times a deadlock occurs due to a wrong order of acquiring locks: multiple threads need to access 2 or more shared resources; each […]

Multi-hashing

Yes I totally invented this term 😛 What I mean by it is producing multiple hashes from a single key. Like this (if the syntax is unfamiliar to you read this): auto [h1, h2, h3] = hashNT(“key”); Or like this (for non-template version which returns a vector): auto hashes = hashN(“key”, 3); Why? One place […]

#pragma

There are two useful #pragma directives I like to use in my code: one let’s the preprocessor know that you want to include a header fine only once, and another deals with structure packing. Instead of using the header include guards, which are ugly as sin, use #pragma once at the beginning of your header […]

Exception safe assignment

Longer title: exception safe assignment operator of resource owning objects. Uff. Because the object owns a resource, how do we write an exception safe assignment operator which will have to free up the old and allocate the new resource. By exception safe I don’t mean that it will never throw, that’s not possible. Instead, I […]

Hashing the C++ way

Modern C++ brought us std::hash template (read more about it here). In short: it’s a stateless function object that implements operator() which takes an instance of a type as parameter and returns its hash as size_t . It has specializations for all primitive types as well as some library types. You can also specialize it […]

Data alignment the C++ way

Before modern C++ the only way to align variables or structures on a given byte boundary was to inject padding; to align a struct to 16 bytes you had to do this: struct Old { int x; char padding[16 – sizeof(int)]; }; Not any more! Modern C++ introduced a keyword just for that: alignas (read […]

Simple file I/O

I was playing around with file I/O the C++ way and decided to create a file hashing program using ifstream and Botan crypto library. The program reads an entire file specified as the command line argument and takes the SHA1 hash of the content. It’s amazing what you can accomplish with well designed frameworks in […]