Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for a snapshot of the master branch, built from commit da4fb7264c.
PrevUpHomeNext

PFR as a C++20 module

[Caution] Caution

C++20 PFR module support is on early stage, targets and flags may change in the future

If using CMake of version 3.28.0 or higher define CMake option -DBUILD_MODULE=1 to make the Boost::pfr_module and Boost::pfr_module_migration libraries available. With Boost::pfr_module C++20 module Boost.PFR could be used:

#include <iostream>
#include <iomanip>
#include <string>

import Boost.PFR;

struct some_person {
    std::string name;
    unsigned birth_year;
};

int main() {
    some_person val{"Edgar Allan Poe", 1809};

    std::cout << boost::pfr::get<0>(val)                // No macro!
        << " was born in " << boost::pfr::get<1>(val);  // Works with any aggregate!

    std::cout << '\n' << boost::pfr::io(val);           // Outputs: {"Edgar Allan Poe", 1809}
    std::cout << "\n." << boost::pfr::get_name<0, some_person>()
        << '=' << val.name << '\n';                     // Outputs: .name=Edgar Allan Poe
}

The Boost::pfr_module_migration CMake target gives an ability to mix includes and imports of the PFR library in different translation units.

If not using CMake, then the module could be build manually from the module/pfr.cppm file. If mixing of includes in imports is desired, additionally define BOOST_PFR_ATTACH_TO_GLOBAL_MODULE preprocessor macro to attach all the module entities to a global module and avoid ODR issues.

For manual module build the following commands could be used for clang compiler:

cd pfr/module
clang++ -I ../include -std=c++20 --precompile -x c++-module pfr.cppm

After that, the module could be used in the following way:

clang++ -std=c++20 -fmodule-file=pfr.pcm pfr.pcm usage_sample.cpp

PrevUpHomeNext