Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Short Examples for the Impatient

struct foo {
    int integer;
    double real;

    void operator +=(int v) {
        integer += v * 10;
        real += v * 100;
    }
};

struct bar {
    char character;
    foo f;
};

bar var{'A', {777, 3.141593}};

Following examples use definition from above:

Code snippet

var content or output

Function description:

// no `std::hash<bar>` or `bar::operator==(const bar&)` defined
std::unordered_set<
    bar, boost::pfr::flat_hash<bar>,
    boost::pfr::flat_equal_to<>> my_uset;
my_uset.insert(var);

my_uset constains var

flat_hash

flat_equal_to

// no `bar::operator<(const bar&)` defined
std::set<bar, boost::pfr::flat_less<>> my_set;
my_set.insert(var);

my_set constains var

flat_less

using namespace boost::pfr::flat_ops; // Defines comparisons
assert((var < bar{'Z', {}} && var.f == foo{777, 3.141593}));

assert succeeds

using namespace flat_ops;

struct test { std::string f1; std::string_view f2; };
using namespace boost::pfr::ops; // Defines comparisons
assert((test{"abc", ""} > test{"aaa", "zomg"}));

assert succeeds

using namespace ops;

// incrementing each field on 1:
boost::pfr::flat_for_each_field(var, [](auto& field) {
    field += 1;
});

var == {B, {778, 4.14159}}

flat_for_each_field

// increments first field on 1, calls foo::operator+= for second field
boost::pfr::for_each_field(var, [](auto& field) {
    field += 1;
});

var == {B, {787, 103.142}}

for_each_field

boost::pfr::flat_for_each_field(var, [](auto& field, std::size_t idx) {
    std::cout << idx << ": "
        << boost::typeindex::type_id_runtime(field) << '\n';
});
0: char
1: int
2: double

flat_for_each_field

boost::pfr::for_each_field(var, [](auto& field, std::size_t idx) {
    std::cout << idx << ": "
        << boost::typeindex::type_id_runtime(field) << '\n';
});
0: char
1: quick_examples_ns::foo

for_each_field

std::cout << "tuple_size: "
    << boost::pfr::tuple_size<bar>::value << '\n';

tuple_size: 2

tuple_size

std::cout << "flat_tuple_size: "
    << boost::pfr::flat_tuple_size<bar>::value << '\n';

flat_tuple_size: 3

flat_tuple_size

boost::pfr::get<1>(var) = foo{1, 2}; // C++17 is required

var == {A {1, 2.0}}

get

boost::pfr::flat_get<1>(var) = 1;

var == {A, {1, 3.14159}}

flat_get

boost::pfr::get<1>(var.f) = 42.01;

var == {A, {777, 42.01}}

get

boost::pfr::flat_get<1>(var.f) = 42.01;

var == {A, {777, 42.01}}

flat_get

// C++17 is required
std::tuple<char, foo> t = boost::pfr::structure_to_tuple(var);
std::get<1>(t) = foo{1, 2};

var == {A, {777, 3.14159}}

structure_to_tuple

std::tuple<char, int, double> t = boost::pfr::flat_structure_to_tuple(var);
std::get<0>(t) = 'C';

var == {A, {777, 3.14159}}

flat_structure_to_tuple

// C++17 is required
std::tuple<char&, foo&> t = boost::pfr::structure_tie(var);
std::get<1>(t) = foo{1, 2};

var == {A, {1, 2.0}}

structure_tie

std::tuple<char&, int&, double&> t = boost::pfr::flat_structure_tie(var);
std::get<0>(t) = 'C';

var == {C, {777, 3.14159}}

flat_structure_tie


PrevUpHomeNext