Skip to content

Instantly share code, notes, and snippets.

View ckwastra's full-sized avatar

Vincent X ckwastra

View GitHub Profile

Implementing assert in CMake

Assertions are powerful tools for verifying function preconditions and postconditions, catching bugs early in development, and ultimately leading to more robust code. Many popular programming languages include built-in support for assertions. Unfortunately, CMake does not currently provide a built-in assert command. While it's arguable that CMake's if command can already cover much of this functionality, a dedicated assert command would be a valuable addition. It offers a more concise and expressive syntax, improving both readability and intent. This post explores several approaches to implementing assert in the CMake language and discusses why it would be best implemented as a built-in CMake command.

The Goal

Our goal is to implement an assert command that can be used by other CMake code. This command should accept the same arguments as the existing if command. It should raise an error if the arguments are invalid or if the evaluated condition is false. In s

Initialization in C++: A Corner Case

Recently, I've been trying to find out the difference between copy-initialization (e.g. in return statements) and direct-initialization (e.g. in static_cast expressions). Besides the explicit keyword, the code posted by Johannes really caught my attention because recent versions of GCC and Clang exhibit different behaviors for the mentioned code. Upon further investigation, it may be surprising that these behaviors are somewhat intentional. In the following, I will try to explain this corner case of initialization in C++. This post may be a bit arcane. Anyway, have fun reading!

Introduction

Given the following code:

// -std=c++23