2
0
mirror of https://github.com/boostorg/hana.git synced 2026-01-21 17:02:25 +00:00
Files
hana/example/tutorial/algorithms.cpp
Louis Dionne ac04435b60 [Doc] Daily improvement of the tutorial
- Merge section on Amphibian Algorithms with section on Algorithms
- Replace the `decayed` pseudo-code by `std::decay_t`.
- Rewrite and move section on type computations
- Add Appendix on the MPL implementation
2015-05-31 18:57:05 -04:00

151 lines
3.1 KiB
C++

/*
@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.hpp>
#include <boost/hana/ext/std/integral_constant.hpp>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
using namespace boost::hana::literals;
using namespace boost::hana;
using namespace std::literals;
int main() {
{
//! [reverse_transform]
auto to_str = [](auto const& x) {
std::stringstream ss;
ss << x;
return ss.str();
};
auto xs = make_tuple(1, 2.2, 'a', "bcde");
BOOST_HANA_RUNTIME_CHECK(
reverse(transform(xs, to_str)) == make_tuple("bcde", "a", "2.2", "1")
);
//! [reverse_transform]
//! [reverse_transform_copy]
reverse(
transform(xs, to_str) // <-- copy into reverse(...) here?
);
//! [reverse_transform_copy]
//! [reverse_transform_move]
reverse(
transform(xs, to_str) // <-- nope, move from the temporary instead!
);
//! [reverse_transform_move]
}{
//! [effects]
auto r = any_of(make_tuple("hello"s, 1.2, 3), [](auto x) {
return std::is_integral<decltype(x)>{};
});
BOOST_HANA_CONSTANT_CHECK(r);
//! [effects]
{
//! [effects.codegen]
auto xs = make_tuple("hello"s, 1.2, 3);
auto pred = [](auto x) { return std::is_integral<decltype(x)>{}; };
auto r = bool_<
decltype(pred(xs[0_c]))::value ? true :
decltype(pred(xs[1_c]))::value ? true :
decltype(pred(xs[2_c]))::value ? true :
false
>;
BOOST_HANA_CONSTANT_CHECK(r);
//! [effects.codegen]
}
}{
//! [cross_phase.setup]
struct Fish { std::string name; };
struct Cat { std::string name; };
struct Dog { std::string name; };
auto animals = make_tuple(Fish{"Nemo"}, Cat{"Garfield"}, Dog{"Snoopy"});
// ^^^^^^^ not a compile-time value
BOOST_HANA_CONSTANT_CHECK(length(animals) == size_t<3>);
// ^^^^^^^^^^^^^^^ assert done at compile-time
//! [cross_phase.setup]
//! [cross_phase.is_empty]
BOOST_HANA_CONSTANT_CHECK(!is_empty(animals));
// ^^^^^^^^^^^^^^^^^ assert done at compile-time
//! [cross_phase.is_empty]
{
//! [cross_phase.any_of_runtime]
bool any_garfield = any_of(animals, [](auto animal) {
return animal.name == "Garfield"s;
});
BOOST_HANA_RUNTIME_CHECK(any_garfield);
//! [cross_phase.any_of_runtime]
}{
//! [cross_phase.any_of_compile_time]
auto any_cat = any_of(animals, [](auto x) {
return std::is_same<decltype(x), Cat>{};
});
BOOST_HANA_CONSTANT_CHECK(any_cat);
//! [cross_phase.any_of_compile_time]
}{
//! [cross_phase.any_of_explicit]
decltype(true_) any_cat = any_of(animals, [](auto x) {
return std::is_same<decltype(x), Cat>{};
});
BOOST_HANA_CONSTANT_CHECK(any_cat);
//! [cross_phase.any_of_explicit]
}{
//! [cross_phase.filter]
auto mammals = filter(animals, [](auto animal) {
return type<decltype(animal)> != type<Fish>;
});
//! [cross_phase.filter]
BOOST_HANA_RUNTIME_CHECK(
transform(mammals, [](auto x) { return x.name; })
== make_tuple("Garfield", "Snoopy")
);
}
}{
//! [cross_phase.std::tuple_size]
std::tuple<int, char, std::string> xs{1, '2', std::string{"345"}};
static_assert(std::tuple_size<decltype(xs)>::value == 3u, "");
//! [cross_phase.std::tuple_size]
}
}