Skip to content

Practical C++26 reflection for Protobuf

Abstract

The new edition of the C++ standard [1], expected to be released in 2026, will add reflection to C++. This will allow developers to write code that will examine the properties of classes, types, functions, and other elements and make changes to them. At the time of writing, reflection is not supported by the main branches of compilers, but it is available in branches such as clang [2] and gcc, which can be build on user's own. For quick experiments, one can also use Compiler Explorer [A].

In review articles and videos on this topic, the most common example is printing arbitrary structures or serializing them in JSON. In this article, the author invites the reader to practice with a slightly more complex example -deserialization with protobuf.

(Continued)

Практична рефлексія C++26 для Protobuf

Вступ

Нова редакція стандарту C++ [1], випуск якої очікується у нинішньому, 2026 році, додасть в C++ рефлексію (reflection). Це дозволить розробникам писати код, який досліджуватиме властивості класів, типів, функцій, та інших елементів і робити у них зміни. На момент написання статті, рефлексія хоча і не підтримується основними гілками компіляторів, проте доступна у відгалуженнях як clang [2] так і gcc, які можна зібрати самостійно. Для експериментів нашвидкоруч можна також скористатись Compiler Explorer [A].

У оглядових статтях та відео на цю тему найпоширеніший приклад - друкування довільних структур чи їх серіалізація в JSON. В рамках цієї статті автор пропонує читачу повправлятися разом з автором із дещо складнішим прикладом - десеріалізацією із protobuf.

(Continued)

Off-target testing embedded projects with STUBMMIO

Introduction

Off-target testing - building and running tests "on a development host or build server rather than on the target hardware" . [3]

stubmmio is a C++ library for off-target embedded unit tests by stubbing MCU’s MMIO regions on a Linux machine.

Memory-mapped I/O (MMIO) is a method of performing I/O through registers, mapped to MCU’s address space [1].

stubmmio allows the users to substitute MMIO registers of interest with the host memory, setup the initial state of
the stubbed MMIO registers, and verify their state against expected after running the code under test.

(Continued)

Logovod – macro-free allocation-free user-configurable logger front-end for C++

1. Features

logovod features

  1. Three styles of use — with functions, ostream left shifts, format string
  2. Automatic space insertion of functional style
  3. Automatic printing of standard containers, tuples, variants, optional
  4. Compile or run-time priority levels
  5. Compile or run-time sinks
  6. Constrained message length
  7. Support for wide-char streams
  8. Priority levels according to RFC-5424 section-6.2.1

(Continued)

is_constexpr – a template to test if a function is constexpr

C++ allows declaring functions as constexpr. Such functions could be evaluated at compile time and used, for example in if constexpr(...). However, a non-constexpr function in this context is not allowed.
So, if we want to write a template, accepting a function and using it in either if constexpr(...) or old plain if(...) we need to test whether the function is constexpr, otherwise compile fails as illustrated below:
(Continued)

setof – an iterable set of strongly typed enumerations

This post suggests an implementation of template class setof which facilitates features of a bit set and a forward-iterable container.

C++ switch with textual cases

C++ does not allow using string literals as case values in a switch statement. In modern C++ this restriction can be worked around with constexpr hash values.
(Continued)

any_of<c1, …, c2>(v)

This simple C++17 template implements constexpr any_of for matching a value against a list of constants.
(Continued)

COJSON library for Arduino

COJSON library is also available as Arduino Studio .ZIP library: cojson.zip.

(Continued)

cojson tutorial for C++17

C++17 has simplified use of template parameters. Instructions, given in this tutorial, describes how to define JSON model with cojson::autos templates.
Note: GCC 7 or higher is required to compile cojson_autos.hpp !

(Continued)