Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] how to get the tuple of min values of 2 tuples whatever their length?
From: Frédéric Bron (frederic.bron_at_[hidden])
Date: 2013-07-18 04:12:43


Thank you all!
It works now with the code below.
What was the most difficult was to find the headers to include.
Frédéric

#include <iostream>
#include <string>

#include <boost/fusion/functional/adapter/fused_function_object.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/transform.hpp>
#include <boost/fusion/include/vector_tie.hpp>
#include <boost/fusion/include/zip_view.hpp>
#include <boost/type_traits/remove_reference.hpp>

typedef boost::fusion::vector<double, int, std::string, char> Key;

template<class SeqOfSeqs, class Func>
typename boost::fusion::result_of::transform<
boost::fusion::zip_view<SeqOfSeqs> const,
boost::fusion::fused_function_object<Func const &> >::type
n_ary_transform(const SeqOfSeqs &s, const Func &f) {
    return boost::fusion::transform(
            boost::fusion::zip_view<SeqOfSeqs>(s),
            boost::fusion::fused_function_object<const Func&>(f));
}

struct min {
    template <typename Sig>
    struct result;

    template <class Self, typename T>
    struct result< Self(T, T) > {
        typedef typename boost::remove_reference<T>::type type;
    };

    template<typename T>
    T operator()(const T &lhs, const T &rhs) const {
        return std::min(lhs, rhs);
    }
};

struct print {
    template <class T>
    void operator()(const T &rhs) const {
        std::cout<<rhs<<'\n';
    }
};

int main() {
    Key a(1., 0, "zz", 'a');
    Key b(2., -1, "aa", 'a');
    Key c;
    c=n_ary_transform(boost::fusion::vector_tie(a, b), min());
    boost::fusion::for_each(c, print());
    return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net