|
Boost : |
Subject: [boost] [fusion] [range] Interest in unified functions
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2013-04-19 05:02:14
Hi,
I was playing with Boost.Fusion and wrote this simple function:
template <class Sequence>
std::string stringize(const Sequence& seq) {
std::string result;
boost::fusion::for_each(seq, stringize_functor(result));
return result;
}
Which allows to convert many types to strings:
boost::fusion::vector<cat, int, std::string> tup1(cat(), 0, "_0");
boost::tuple<cat, int, std::string> tup2(cat(), 0, "_0");
std::pair<cat, cat> cats;
std::cout << stringize(tup1) << '\n'
<< stringize(tup2) << '\n'
<< stringize(cats) << '\n'
But this example fails to convert the following : std::vector<cat>
too_many_cats(20);
All we need to do to allow conversions, is just add the following:
#include <boost/range/algorithm/for_each.hpp>
namespace boost { namespace fusion {
template <typename Sequence, typename F>
inline
typename
disable_if<
traits::is_sequence<Sequence>
, void
>::type
for_each(Sequence const& seq, F const& f)
{
::boost::range::for_each(seq, f);
}
}}
So now std::cout << stringize(too_many_cats) << '\n'; works and
boost::fusion::for_each may work with any type, runtime length or
compile time.
We may also make a lot of other algorithms to make no difference
between runtime and compile time classes: accumulate, find, find_if,
count, all, none, begin, end, advance, distance, dereference, empty
and others...
The questions are:
Is there interest in such features?
Is there interest in adopting other algorithms and which ones?
To what library shall those methods belong?
-- Best regards, Antony Polukhin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk