• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Louis Dionne 2013-2017
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/assert.hpp>
6 #include <boost/hana/at_key.hpp>
7 #include <boost/hana/define_struct.hpp>
8 #include <boost/hana/string.hpp>
9 
10 #include <string>
11 namespace hana = boost::hana;
12 
13 
14 struct Person {
15     BOOST_HANA_DEFINE_STRUCT(Person,
16         (std::string, name),
17         (std::string, last_name),
18         (int, age)
19     );
20 };
21 
main()22 int main() {
23     // non-const ref
24     {
25         Person john{"John", "Doe", 30};
26         std::string& name = hana::at_key(john, BOOST_HANA_STRING("name"));
27         std::string& last_name = hana::at_key(john, BOOST_HANA_STRING("last_name"));
28         int& age = hana::at_key(john, BOOST_HANA_STRING("age"));
29 
30         name = "Bob";
31         last_name = "Foo";
32         age = 99;
33 
34         BOOST_HANA_RUNTIME_CHECK(john.name == "Bob");
35         BOOST_HANA_RUNTIME_CHECK(john.last_name == "Foo");
36         BOOST_HANA_RUNTIME_CHECK(john.age == 99);
37     }
38 
39     // const ref
40     {
41         Person john{"John", "Doe", 30};
42         Person const& const_john = john;
43         std::string const& name = hana::at_key(const_john, BOOST_HANA_STRING("name"));
44         std::string const& last_name = hana::at_key(const_john, BOOST_HANA_STRING("last_name"));
45         int const& age = hana::at_key(const_john, BOOST_HANA_STRING("age"));
46 
47         john.name = "Bob";
48         john.last_name = "Foo";
49         john.age = 99;
50 
51         BOOST_HANA_RUNTIME_CHECK(name == "Bob");
52         BOOST_HANA_RUNTIME_CHECK(last_name == "Foo");
53         BOOST_HANA_RUNTIME_CHECK(age == 99);
54     }
55 }
56