1// Copyright (c) 2020-2024 Antony Polukhin
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6
7#include <boost/pfr/core.hpp>
8
9#include <boost/core/lightweight_test.hpp>
10
11namespace testing {
12
13namespace {
14
15struct Aggregate {
16 int a;
17 const int b;
18 double c;
19 double d;
20 short e;
21};
22
23void test_get_by_type() {
24#if BOOST_PFR_USE_CPP17
25 Aggregate t{.a: 1, .b: 2, .c: 3.4, .d: 5.6, .e: 7};
26
27 BOOST_TEST_EQ(boost::pfr::get<int>(t), 1);
28 BOOST_TEST_EQ(boost::pfr::get<const int>(t), 2);
29 BOOST_TEST_EQ(boost::pfr::get<short>(t), 7);
30
31 boost::pfr::get<int>(val&: t) = 11;
32 boost::pfr::get<short>(val&: t) = 77;
33#endif
34}
35
36void test_const_get_by_type() {
37#if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
38 const Aggregate t{.a: 1, .b: 2, .c: 3.4, .d: 5.6, .e: 7};
39 BOOST_TEST_EQ(boost::pfr::get<short>(t), 7);
40#endif
41}
42
43void test_get_by_type_pod() {
44 struct PodAggregate {
45 int i;
46 short s;
47 };
48
49 PodAggregate pod{.i: 1, .s: 2};
50 BOOST_TEST_EQ(boost::pfr::get<int>(pod), 1);
51 BOOST_TEST_EQ(boost::pfr::get<short>(pod), 2);
52}
53
54} // anonymous namespace
55
56
57} // namespace testing
58
59int main() {
60 testing::test_get_by_type();
61 testing::test_const_get_by_type();
62 testing::test_get_by_type_pod();
63
64 return boost::report_errors();
65}
66
67
68

source code of boost/libs/pfr/test/core/run/get_by_type.cpp