Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Short Examples for the Impatient

Code snippet

Reference:

// Get field by index/type and assign new value to that field

struct sample {
    char c;
    float f;
};

sample var{};
pfr::get<1>(var) = 42.01f;
pfr::get<char>(var) = 'A';

std::cout << var.c << var.f; // Outputs: A 42.01

pfr::get

// Get name of field by index

struct sample {
    int f1;
    long f2;
};

std::cout << pfr::get_name<0, sample>()
          << pfr::get_name<1, sample>(); // Outputs: f1 f2

pfr::get_name

// Assert equality.
// Note that the equality operator for structure is not defined.

struct test {
    std::string f1;
    std::string_view f2;
};

assert(
    pfr::eq(test{"aaa", "zomg"}, test{"aaa", "zomg"})
);

pfr/ops.hpp:

* pfr::eq

* pfr::ne

* pfr::gt

* ...

// Increment each field of the variable on 1 and
// output the content of the variable.

struct test {
    int f1;
    long f2;
};

test var{42, 43};

pfr::for_each_field(var, [](auto& field) {
    field += 1;
});

// Outputs: {43, 44}
std::cout << pfr::io(var);

pfr::for_each_field

pfr::io

// Define all the comparison and IO operators for my_structure type along
// with hash_value function.

#include <pfr/functions_for.hpp>

namespace my_namespace {
    struct my_structure {
        int a,b,c,d,e,f,g;
        // ...
    };
    PFR_FUNCTIONS_FOR(my_structure)
}

PFR_FUNCTIONS_FOR

// Define only the equality and inequality operators for my_eq_ne_structure.

#include <pfr/functions_for.hpp>

namespace my_namespace {
    struct my_eq_ne_structure {
        float a,b,c,d,e,f,g;
        // ...
    };

    inline bool operator==(const my_eq_ne_structure& x, const my_eq_ne_structure& y) {
        return pfr::eq_fields(x, y);
    }

    inline bool operator!=(const my_eq_ne_structure& x, const my_eq_ne_structure& y) {
        return pfr::ne_fields(x, y);
    }
}

pfr/ops_fields.hpp:

* pfr::eq_fields

* pfr::ne_fields

* pfr::gt_fields

* ...

pfr/io_fields.hpp

* pfr::io_fields

// Iterate over fields of a variable and output index and
// type of a variable.

struct tag0{};
struct tag1{};
struct sample {
    tag0 a;
    tag1 b;
};

// Outputs:
//  0: tag0
//  1: tag1
pfr::for_each_field(sample{}, [](const auto& field, std::size_t idx) {
    std::cout << '\n' << idx << ": "
        << boost::typeindex::type_id_runtime(field);
});

pfr::for_each_field

// Getting fields count of some structure

struct some { int a,b,c,d,e; };

std::cout << "Fields count in structure: "
    << pfr::tuple_size<some>::value  // Outputs: 5
    << '\n';

pfr::tuple_size

// Getting a std::tuple of values from structures fields

struct foo { int a, b; };
struct other {
    char c;
    foo nested;
};

other var{'A', {3, 4}};
std::tuple<char, foo> t = pfr::structure_to_tuple(var);
assert(std::get<0>(t) == 'A');
assert(
    pfr::eq(std::get<1>(t), foo{3, 4})
);

pfr::structure_to_tuple

// Getting a std::tuple of references to structure fields

struct foo { int a, b; };
struct other {
    char c;
    foo f;
};

other var{'A', {14, 15}};
std::tuple<char&, foo&> t = pfr::structure_tie(var);
std::get<1>(t) = foo{1, 2};

std::cout << pfr::io(var.f); // Outputs: {1, 2}

pfr::structure_tie


PrevUpHomeNext