Boost C++ Libraries Home Libraries People FAQ More

Next

Chapter 1. Boost.Precise and Flat Reflection 1.0

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Table of Contents

Motivation
Tutorial
Accessing POD member by index
Flattening
Flat or Precise functions to choose
Three ways of getting operators
Reflection of unions
Short Examples for the Impatient
Configuration Macro
Requirements and Limitations
How it works
Acknowledgements
Reference
Precise Reflection
Flat Reflection
Include all

In C++ we have:

  • tuples - types that provide access to members by index. Those are useful for generic programming.
  • structures - types with named fields that do not provide access to members by index. Those are just easy to use.

This library provides tuple like methods for aggregate initializable structures, making them usable in contexts were only tuples were useful.

[Note] Note

All you have to do is to add #include <boost/pfr.hpp>.

No macro or other type/member registrations required.

The two different types of reflection are:

  • Precise reflection, where each field type is represented as it actually exists, even if the type is itself a user-defined type.
  • Flat reflection, where user-defined types are represented by their individual field types, and all other types are represented as they actually exists.

As an example:

#include <boost/pfr.hpp>

struct nested_t { char c; };
struct foo_t { int i; nested_t nested; };


static_assert(std::is_same<
    boost::pfr::flat_tuple_element_t<1, foo_t>, // Flat reflection.
    char    // `char`, not `nested_t`!
>::value, "");


// Requires C++17 or Loophole enabled:
static_assert(std::is_same<
    boost::pfr::tuple_element_t<1, foo_t>,      // Precise reflection.
    nested_t
>::value, "");

Boost.Precise and Flat Reflection (Boost.PFR) adds following out-of-the-box functionality for aggregate initializable structures:

  • comparison operators
  • heterogeneous comparators
  • hash
  • stream operators
  • access to members by index
  • member reflections
  • methods for cooperation with std::tuple
  • methods to visit each field of the structure
[Warning] Warning

This is not an official Boost library! It wasn't reviewed and can't be downloaded from www.boost.org. This library is available to the community to know real interest and get comments for refinement. The intention is to submit library to formal review, if community think that it is interesting!

[Caution] Caution

This is a C++14 library! Pre C++14 compilers (C++11, C++03...) are not supported

Last revised: January 30, 2019 at 20:06:55 GMT


Next