mirror of
https://github.com/boostorg/hana.git
synced 2026-02-01 08:32:11 +00:00
[Tuple] Add examples and slightly improve tests
This commit is contained in:
79
example/tuple.cpp
Normal file
79
example/tuple.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
@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)
|
||||
*/
|
||||
|
||||
#include <boost/hana/assert.hpp>
|
||||
#include <boost/hana/detail/constexpr.hpp>
|
||||
#include <boost/hana/ext/std/array.hpp>
|
||||
#include <boost/hana/ext/std/tuple.hpp>
|
||||
#include <boost/hana/foldable.hpp>
|
||||
#include <boost/hana/integral_constant.hpp>
|
||||
#include <boost/hana/logical.hpp>
|
||||
#include <boost/hana/range.hpp>
|
||||
#include <boost/hana/sequence.hpp>
|
||||
#include <boost/hana/tuple.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <tuple>
|
||||
using namespace boost::hana;
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
{
|
||||
|
||||
//! [tuple_t]
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
to<Tuple>(tuple_t<int, char, void, int(float)>)
|
||||
==
|
||||
tuple(type<int>, type<char>, type<void>, type<int(float)>)
|
||||
);
|
||||
//! [tuple_t]
|
||||
|
||||
}{
|
||||
|
||||
//! [tuple_c]
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
to<Tuple>(tuple_c<int, 0, 1, 2>)
|
||||
==
|
||||
tuple(int_<0>, int_<1>, int_<2>)
|
||||
);
|
||||
BOOST_HANA_CONSTANT_CHECK(head(tuple_c<int, 0, 1, 2>) == int_<0>);
|
||||
//! [tuple_c]
|
||||
|
||||
}{
|
||||
|
||||
//! [Foldable]
|
||||
using namespace literals;
|
||||
|
||||
constexpr auto numbers = tuple_c<int, 5, -1, 0, -7, -2, 0, -5, 4>;
|
||||
constexpr auto negatives = tuple_c<int, -1, -7, -2, -5>;
|
||||
|
||||
BOOST_HANA_CONSTEXPR_LAMBDA auto keep_negatives = [](auto n, auto acc) {
|
||||
return if_(n < 0_c, prepend(n, acc), acc);
|
||||
};
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(foldr(numbers, tuple_c<int>, keep_negatives) == negatives);
|
||||
//! [Foldable]
|
||||
|
||||
}{
|
||||
|
||||
//! [Tuple_interop]
|
||||
BOOST_HANA_CONSTEXPR_CHECK(
|
||||
to<Tuple>(std::make_tuple(1, '2', 3.3)) == tuple(1, '2', 3.3)
|
||||
);
|
||||
|
||||
BOOST_HANA_CONSTANT_CHECK(
|
||||
to<Tuple>(range(int_<1>, int_<4>)) == tuple(int_<1>, int_<2>, int_<3>)
|
||||
);
|
||||
|
||||
BOOST_HANA_CONSTEXPR_CHECK(
|
||||
to<Tuple>(std::array<int, 3>{{1, 2, 3}}) == tuple(1, 2, 3)
|
||||
);
|
||||
//! [Tuple_interop]
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user