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

PFR as a C++20 module
PrevUpHomeNext
[Caution] Caution

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

If using modern CMake define CMake option -DBOOST_USE_MODULES=1 to build a C++20 module and make the Boost::pfr CMake target provide it. After that an explicit usage of C++20 module boost.pfr is allowed:

#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 CMake target gives an ability to mix includes and imports of the PFR library in different translation units. Moreover, if BOOST_USE_MODULES macro is defined then all the boost/pfr/... includes implicilty do import boost.pfr; to give all the benifits of modules without changing the existing code.

[Note] Note

For better compile times make sure that import std; is available when building the boost.pfr module (in CMake logs there should be a 'Using import std;' message).

If not using CMake, then the module could be build manually from the modules/boost_pfr.cppm file.

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

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

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

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

PrevUpHomeNext