C++ - Reflection example

less than 1 minute read

Practical Reflection With C++26 - Barry Revzin - CppCon 2025

C++ Data oriented programming with reflection

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
28
29
30
31
32
33
#include <experimental/meta> // Experimental reflection header
#include <vector>
#include <cstddef>

template <class T>
class SoaVector {
    // 1. Declare the nested struct that will hold our data
    struct Storage;

    // 2. The consteval block executes at compile-time to "reflect" on T
    consteval {
        std::vector<std::meta::info> specs;

        // Loop through all non-static data members of the template type T
        for (auto m : nonstatic_data_members_of(^^T)) {
            // For each member in T, add a pointer of that type to our Storage
            specs.push_back(data_member_spec(
                add_pointer(type_of(m)), 
                {.name = identifier_of(m)}
            ));
        }

        // Add the bookkeeping members to the Storage struct
        specs.push_back(data_member_spec(^^std::size_t, {.name = "size_"}));
        specs.push_back(data_member_spec(^^std::size_t, {.name = "capacity_"}));

        // 3. Define the 'Storage' struct using the collected specifications
        define_aggregate(^^Storage, specs);
    }

    // Instantiate the dynamically generated Storage struct
    Storage storage_ = {};
};

Categories:

Updated: