2
0
mirror of https://github.com/boostorg/hana.git synced 2026-01-22 05:12:35 +00:00
Files
hana/example/tutorial/algorithms.cpp
Louis Dionne 32d4c973e9 [Doc] Write new tutorial sections and reorder other sections
- Write sections on containers, algorithms, and runtime performance
- Make sure examples consistently use 2 space tabs
- Disable -Wunused-parameter for examples
- Resolves #70
  The requirement of pure functions is now documented.
- Resolves #14
  It turns out that the benefits of specifying the type of containers
  seems to be essentially limited to pattern matching. This is not
  enough to justify all the bad things it brings, especially
  considering the fact that recursion (currently the only use
  case for pattern matching) forces the creation of a new tuple
  at each step, which is disastrous. The unspecified-ness of the
  container's type is now documented.
2015-05-28 17:03:46 -04:00

58 lines
1.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/type_traits.hpp>
#include <sstream>
#include <string>
#include <type_traits>
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)>{};
});
static_assert(r, "");
//! [effects]
}
}