2
0
mirror of https://github.com/boostorg/hana.git synced 2026-01-22 17:22:30 +00:00
Files
hana/example/overview.cpp
2015-02-01 10:35:17 -05:00

43 lines
1.3 KiB
C++

/*
@copyright Louis Dionne 2014
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
//////////////////////////////////////////////////////////////////////////////
// Important: Keep this file in sync with the Overview in the README
//////////////////////////////////////////////////////////////////////////////
#include <boost/hana/assert.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
#include <string>
using namespace boost::hana;
struct President { std::string name; };
struct Car { std::string name; };
struct City { std::string name; };
int main() {
// Heterogeneous sequences for value-level metaprogramming.
auto stuff = tuple(President{"Obama"}, Car{"Toyota"}, City{"Quebec"});
auto names = transform(stuff, [](auto thing) { return thing.name; });
BOOST_HANA_RUNTIME_CHECK(reverse(names) == tuple("Quebec", "Toyota", "Obama"));
// No compile-time information is lost:
// `stuff` wasn't constexpr but its length is!
static_assert(length(stuff) == 3u, "");
// Type-level metaprogramming works too.
auto types = transform(stuff, [](auto thing) {
return type<decltype(thing)*>;
});
BOOST_HANA_CONSTANT_CHECK(
types == tuple(type<President*>, type<Car*>, type<City*>)
);
}