| 1 | // Copyright Louis Dionne 2013-2022 |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 4 | |
| 5 | // Make sure `assert` always triggers an assertion |
| 6 | #ifdef NDEBUG |
| 7 | # undef NDEBUG |
| 8 | #endif |
| 9 | |
| 10 | #include <boost/fusion/include/comparison.hpp> |
| 11 | #include <boost/fusion/include/make_vector.hpp> |
| 12 | #include <boost/fusion/include/transform.hpp> |
| 13 | #include <boost/fusion/include/vector.hpp> |
| 14 | |
| 15 | #include <boost/mpl/equal.hpp> |
| 16 | #include <boost/mpl/placeholders.hpp> |
| 17 | #include <boost/mpl/transform.hpp> |
| 18 | #include <boost/mpl/vector.hpp> |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cassert> |
| 22 | #include <iterator> |
| 23 | #include <sstream> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | namespace fusion = boost::fusion; |
| 27 | namespace mpl = boost::mpl; |
| 28 | using namespace std::literals; |
| 29 | |
| 30 | |
| 31 | int main() { |
| 32 | |
| 33 | { |
| 34 | |
| 35 | //! [runtime] |
| 36 | auto f = [](int i) -> std::string { |
| 37 | return std::to_string(val: i * i); |
| 38 | }; |
| 39 | |
| 40 | std::vector<int> ints{1, 2, 3, 4}; |
| 41 | std::vector<std::string> strings; |
| 42 | std::transform(first: ints.begin(), last: ints.end(), result: std::back_inserter(x&: strings), unary_op: f); |
| 43 | |
| 44 | assert((strings == std::vector<std::string>{"1" , "4" , "9" , "16" })); |
| 45 | //! [runtime] |
| 46 | |
| 47 | }{ |
| 48 | |
| 49 | //! [heterogeneous] |
| 50 | auto to_string = [](auto t) { |
| 51 | std::stringstream ss; |
| 52 | ss << t; |
| 53 | return ss.str(); |
| 54 | }; |
| 55 | |
| 56 | fusion::vector<int, std::string, float> seq{1, "abc" , 3.4f}; |
| 57 | fusion::vector<std::string, std::string, std::string> |
| 58 | strings = fusion::transform(seq, f: to_string); |
| 59 | |
| 60 | assert(strings == fusion::make_vector("1"s , "abc"s , "3.4"s )); |
| 61 | //! [heterogeneous] |
| 62 | |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | //! [type-level] |
| 68 | template <typename T> |
| 69 | struct add_const_pointer { |
| 70 | using type = T const*; |
| 71 | }; |
| 72 | |
| 73 | using types = mpl::vector<int, char, float, void>; |
| 74 | using pointers = mpl::transform<types, add_const_pointer<mpl::_1>>::type; |
| 75 | |
| 76 | static_assert(mpl::equal< |
| 77 | pointers, |
| 78 | mpl::vector<int const*, char const*, float const*, void const*> |
| 79 | >::value, "" ); |
| 80 | //! [type-level] |
| 81 | |