boost-no-inspect
| 14 | |
| 15 | // boost-no-inspect |
| 16 | void test_examples() { |
| 17 | |
| 18 | #if BOOST_PFR_USE_CPP17 |
| 19 | { |
| 20 | //[pfr_quick_examples_ops |
| 21 | // Assert equality. |
| 22 | // Note that the equality operator for structure is not defined. |
| 23 | |
| 24 | struct test { |
| 25 | std::string f1; |
| 26 | std::string_view f2; |
| 27 | }; |
| 28 | |
| 29 | assert( |
| 30 | boost::pfr::eq(test{"aaa", "zomg"}, test{"aaa", "zomg"}) |
| 31 | ); |
| 32 | //] |
| 33 | } |
| 34 | #endif |
| 35 | |
| 36 | { |
| 37 | //[pfr_quick_examples_for_each |
| 38 | // Increment each field of the variable on 1 and |
| 39 | // output the content of the variable. |
| 40 | |
| 41 | struct test { |
| 42 | int f1; |
| 43 | long f2; |
| 44 | }; |
| 45 | |
| 46 | test var{42, 43}; |
| 47 | |
| 48 | boost::pfr::for_each_field(var, [](auto& field) { |
| 49 | field += 1; |
| 50 | }); |
| 51 | |
| 52 | // Outputs: {43, 44} |
| 53 | std::cout << boost::pfr::io(var); |
| 54 | //] |
| 55 | } |
| 56 | |
| 57 | { |
| 58 | //[pfr_quick_examples_for_each_idx |
| 59 | // Iterate over fields of a variable and output index and |
| 60 | // type of a variable. |
| 61 | |
| 62 | struct tag0{}; |
| 63 | struct tag1{}; |
| 64 | struct sample { |
| 65 | tag0 a; |
| 66 | tag1 b; |
| 67 | }; |
| 68 | |
| 69 | // Outputs: |
| 70 | // 0: tag0 |
| 71 | // 1: tag1 |
| 72 | boost::pfr::for_each_field(sample{}, [](const auto& field, std::size_t idx) { |
| 73 | std::cout << '\n' << idx << ": " |
no test coverage detected