C++ рефлексія: Як ітерувати поля структури за допомогою Boost::PFR

reflection_iter_fields.cpp
#include <boost/pfr.hpp>
#include <iostream>
#include <string>

struct Person {
    int id;
    std::string name;
    double score;
};

int main() {
    Person p{42, "Alice", 98.7};

    // Ітерувати поля як кортеж
    boost::pfr::for_each_field(p, [](const auto& field, std::size_t i) {
        std::cout << "Поле #" << i << " = " << field << '\n';
    });

    return 0;
}

Вивід

reflection_output.txt
Поле #0 = 42
Поле #1 = Alice
Поле #2 = 98.7

Дивіться схожі статті за категоріями: C/C++