1// Copyright Louis Dionne 2013-2022
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4
5#include <boost/hana/accessors.hpp>
6#include <boost/hana/assert.hpp>
7#include <boost/hana/at.hpp>
8#include <boost/hana/define_struct.hpp>
9#include <boost/hana/equal.hpp>
10#include <boost/hana/first.hpp>
11#include <boost/hana/integral_constant.hpp>
12#include <boost/hana/second.hpp>
13#include <boost/hana/string.hpp>
14
15#include <string>
16namespace hana = boost::hana;
17
18
19struct Person {
20 BOOST_HANA_DEFINE_STRUCT(Person,
21 (std::string, name),
22 (unsigned short, age)
23 );
24};
25
26int main() {
27 constexpr auto accessors = hana::accessors<Person>();
28 BOOST_HANA_CONSTANT_CHECK(
29 hana::first(accessors[hana::size_c<0>]) == BOOST_HANA_STRING("name")
30 );
31
32 BOOST_HANA_CONSTANT_CHECK(
33 hana::first(accessors[hana::size_c<1>]) == BOOST_HANA_STRING("age")
34 );
35
36 constexpr auto get_name = hana::second(accessors[hana::size_c<0>]);
37 constexpr auto get_age = hana::second(accessors[hana::size_c<1>]);
38
39 Person john{.name: "John", .age: 30};
40 BOOST_HANA_RUNTIME_CHECK(get_name(john) == "John");
41 BOOST_HANA_RUNTIME_CHECK(get_age(john) == 30);
42}
43

source code of boost/libs/hana/example/accessors.cpp