2
0
mirror of https://github.com/boostorg/hana.git synced 2026-01-29 19:42:12 +00:00
Files
hana/example/tutorial/introduction.cpp
Louis Dionne 35f25cbc35 [Doc] Implement improvements suggested by @tzlaine (part 1)
- Remove the preface entirely
- Move the current introduction to a "Description" section
- Move the "Quadrants of computation" to the "Introduction"
- Move the current "Motivation" section to the start of the "Introduction"
- Move the section on "Introspection" higher up in the docs
- Remove the "Concept" column in the Cheatsheet, and add links to each
  individual algorithm instead.
- Add a rationale for unpack's name and parameter order
- Add `fold` and `reverse_fold` to the cheatsheet
- Mention the list of all headers in the tutorial
- Add an alphabetical index with everything in the library
- Precise that smaller is better in chart subtitles
- Add benchmarks for std::make_tuple and std::get
2015-06-04 21:13:45 -04:00

86 lines
1.9 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)
*/
// Make sure `assert` always triggers an assertion
#ifdef NDEBUG
# undef NDEBUG
#endif
#include <boost/hana.hpp>
#include <boost/fusion/include/comparison.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/include/transform.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/vector.hpp>
#include <algorithm>
#include <cassert>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using namespace boost::hana;
namespace fusion = boost::fusion;
namespace mpl = boost::mpl;
using namespace std::literals;
int main() {
{
//! [runtime]
auto f = [](int i) -> std::string {
return std::to_string(i * i);
};
std::vector<int> ints{1, 2, 3, 4};
std::vector<std::string> strings;
std::transform(ints.begin(), ints.end(), std::back_inserter(strings), f);
assert((strings == std::vector<std::string>{"1", "4", "9", "16"}));
//! [runtime]
}{
//! [heterogeneous]
auto to_string = [](auto t) {
std::stringstream ss;
ss << t;
return ss.str();
};
fusion::vector<int, std::string, float> seq{1, "abc", 3.4f};
fusion::vector<std::string, std::string, std::string>
strings = fusion::transform(seq, to_string);
assert(strings == fusion::make_vector("1"s, "abc"s, "3.4"s));
//! [heterogeneous]
}
}
//! [type-level]
template <typename T>
struct add_const_pointer {
using type = T const*;
};
using types = mpl::vector<int, char, float, void>;
using pointers = mpl::transform<types, add_const_pointer<mpl::_1>>::type;
static_assert(mpl::equal<
pointers,
mpl::vector<int const*, char const*, float const*, void const*>
>::value, "");
//! [type-level]