mirror of
https://github.com/boostorg/hana.git
synced 2026-02-02 08:52:11 +00:00
[Map] Provide model of Foldable, improve docs and add insert method.
Fixes #41
This commit is contained in:
44
example/map.from_record.cpp
Normal file
44
example/map.from_record.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
@copyright Louis Dionne 2015
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
|
||||
#include <boost/hana/assert.hpp>
|
||||
#include <boost/hana/config.hpp>
|
||||
#include <boost/hana/map.hpp>
|
||||
#include <boost/hana/pair.hpp>
|
||||
#include <boost/hana/string.hpp>
|
||||
#include <boost/hana/tuple.hpp>
|
||||
|
||||
#include <string>
|
||||
using namespace boost::hana;
|
||||
|
||||
|
||||
//! [main]
|
||||
struct Person {
|
||||
std::string name;
|
||||
int age;
|
||||
|
||||
struct hana { struct members_impl {
|
||||
static BOOST_HANA_CONSTEXPR_LAMBDA auto apply() {
|
||||
return make<Tuple>(
|
||||
make<Pair>(BOOST_HANA_STRING("name"), [](auto&& p) {
|
||||
return p.name;
|
||||
}),
|
||||
make<Pair>(BOOST_HANA_STRING("age"), [](auto&& p) {
|
||||
return p.age;
|
||||
})
|
||||
);
|
||||
}
|
||||
};};
|
||||
};
|
||||
|
||||
int main() {
|
||||
Person john{"John", 35};
|
||||
BOOST_HANA_RUNTIME_ASSERT(to<Map>(john) == make<Map>(
|
||||
make<Pair>(BOOST_HANA_STRING("name"), "John"),
|
||||
make<Pair>(BOOST_HANA_STRING("age"), 35)
|
||||
));
|
||||
}
|
||||
//! [main]
|
||||
Reference in New Issue
Block a user