Skip to content

Instantly share code, notes, and snippets.

@ricejasonf
Created Sep 18, 2017
Embed
What would you like to do?
Boost.Fit Review Samples
#include <boost/fit/fold.hpp>
#include <boost/fit/alias.hpp>
#include <iostream>
#include <string>
namespace fit = boost::fit;
struct my_string_tag { };
using my_string = fit::alias_inherit<std::string, my_string_tag>;
constexpr auto plus = [](auto const& x, auto const& y) { return x + y; };
constexpr auto sum = fit::fold(plus);
int main()
{
my_string result = sum(
my_string("Hello")
, my_string(", ")
, my_string("world!")
);
std::cout << result << '\n';
}
#include <boost/fit/alias.hpp>
#include <boost/fit/arg.hpp>
#include <boost/fit/by.hpp>
#include <boost/fit/decay.hpp>
#include <boost/fit/partial.hpp>
#include <boost/fit/returns.hpp>
#include <iostream>
#include <vector>
namespace fit = boost::fit;
struct my_int_tag { };
using my_int = fit::alias<int, my_int_tag>;
constexpr auto make_vector = [](auto&& ...x) BOOST_FIT_RETURNS(
std::vector<decltype(fit::decay(fit::arg_c<1>(x...)))>{
x...
}
);
constexpr auto transform_in_place = [](auto const& f, auto& x)
{ return std::transform(x.begin(), x.end(), x.begin(), f); };
constexpr auto inc = [](auto x) -> decltype(x) { return fit::alias_value(x) + 1; };
constexpr auto inc_all = fit::partial(transform_in_place)(inc);
int main()
{
auto nums = make_vector(
my_int{0}
, my_int{1}
, my_int{2}
, my_int{3}
);
inc_all(nums);
for (auto const& x : nums)
{
std::cout << fit::alias_value(x) << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment