...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Caution | |
---|---|
Recommended C++ Standards are C++20 and above. C++17 completely enough for a user who doesn't want accessing name of structure member. Library requires at least C++14! Pre C++14 compilers (C++11, C++03...) are not supported. |
Boost.PFR library works with types that satisfy the requirements of SimpleAggregate
: aggregate types without
base classes, const
fields, references,
or C arrays:
struct simple_aggregate { // SimpleAggregate std::string name; int age; boost::uuids::uuid uuid; }; struct empty { // SimpleAggregate }; struct aggregate : empty { // not a SimpleAggregate std::string name; int age; boost::uuids::uuid uuid; };
The library may work with aggregates that don't satisfy the requirements of
SimpleAggregate
, but the behavior
tends to be non-portable.
Boost.PFRs extraction of field name works with only SimpleAggregate
types.
By default Boost.PFR auto-detects your compiler abilities and automatically defines the configuration macro into appropriate values. If you wish to override that behavior, just define:
Table 27.2. Macros
Macro name |
Effect |
---|---|
BOOST_PFR_USE_CPP17 |
Define to |
BOOST_PFR_USE_LOOPHOLE |
Define to |
BOOST_PFR_USE_STD_MAKE_INTEGRAL_SEQUENCE |
Define to |
BOOST_PFR_HAS_GUARANTEED_COPY_ELISION |
Define to |
BOOST_PFR_ENABLE_IMPLICIT_REFLECTION |
Define to |
BOOST_PFR_CORE_NAME_ENABLED |
On platforms where field name extraction is not supported, the 'boost/pfr/config.hpp' header defines the BOOST_PFR_CORE_NAME_ENABLED macro equal to 0. Defining this macro as 0 before including the header disables the ability to get a field name. |
BOOST_PFR_FUNCTION_SIGNATURE |
For known compilers defined to a compiler specific macro, that outputs the whole function signature including non-type template parameters. |
BOOST_PFR_CORE_NAME_PARSING |
Describes extraction of field name from BOOST_PFR_FUNCTION_SIGNATURE macro. See details below. |
BOOST_PFR_ENABLED |
On platforms where Boost.PFR is not supported, the |
The Boost.PFRs reflection has some limitations that depend on a C++ Standard and compiler capabilities:
boost::pfr::get
,
boost::pfr::structure_to_tuple
,
boost::pfr::structure_tie
,
boost::pfr::tuple_element
require T to be a POD type with built-in types only.
The Boost.PFRs extraction of field name has some limitations that depend on a C++ Standard and compiler capabilities:
extern T t;
, i.e. has a non-internal linkage.
BOOST_PFR_CORE_NAME_PARSING
is already set up for most of the popular compilers. You need to adjust it
only if some static_assert in the library complained on BOOST_PFR_CORE_NAME_PARSING
.
To do that:
test/core_name/print_name.cpp
with your compiler and run it
(skip_at_begin,
skip_at_end,
"")
,
where
skip_at_begin
is
equal to characters count before the first occurrence of user_defined_field
in output
skip_at_end
is equal
to characters count after last occurrence of user_defined_field
in output
test/core_name/print_name.cpp
returns "user_defined_field"
user_defined_field
,
then define BOOST_PFR_CORE_NAME_PARSING to (skip_at_begin,
skip_at_end,
"T = ")
,
where
skip_at_begin
is
equal to skip_at_begin
at step 2
skip_at_end
is equal
to skip_at_end
at
step 2
"T = "
is equal
to characters that are right before the user_defined_field
in output, use backward("T = ")
to search for the occurange in the
string from the right
BOOST_PFR_CORE_NAME_PARSING
macro and the initial output of test/core_name/print_name.cpp
.