我正在尝试让自己熟悉 boost::hana。作为练习,我想创建一个函数,该函数将使用用户提供的比较函数从 hana::tuple 中删除重复项。我面临的问题与使用hana::type_c
's 将类型存储为对象有关。这是我所拥有的
#include <boost/hana/equal.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/unpack.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/any_of.hpp>
#include <boost/hana/second.hpp>
#include <boost/hana/fold.hpp>
#include <boost/hana/core/make.hpp>
#include <boost/hana/core/tag_of.hpp>
#include <iostream>
template <class>
struct what_is;
namespace hana = boost::hana;
// simply push back an element to the sequence
auto push_back = [](auto seq, auto t) {
using namespace boost::hana;
return unpack(seq, [&](auto&&... element){return make<typename tag_of<decltype(seq)>::type>(element..., t);});
};
// this is the main function
auto remove_duplicates = [](auto seq, auto comp) {
using namespace boost::hana;
auto f = [&](auto state, auto el){
return if_( any_of(state, partial(comp, el)),
[=](){return state;},
[=](){return push_back(state, el);})();
};
return fold(seq, make<typename tag_of<decltype(seq)>::type>(), f);
};
// user-defined comparison function
// elements are considered equal if only second element of pairs are equal
auto comp_pair = [](auto&& t1, auto&& t2) {
using namespace boost::hana;
return equal(second(t1), second(t2));
};
int main() {
auto my_tuple1 = hana::tuple_t<int, float, double, int, float>;
auto no_dups1 = remove_duplicates(my_tuple1, hana::equal); // this is fine, decltype(no_dups1) -> tuple< type<int>, type<float>, type<double> >
auto my_tuple2 = hana::tuple_t< hana::pair<int, int>, hana::pair<float, int>, hana::pair<float, float> >;
// auto no_dups2 = remove_duplicates(my_tuple2, comp_pair); // what I want here is tuple< type<pair<int, int>>, type<pair<float, float>> >
}
最后一行产生了问题,因为没有第二个元素可以从 a 中提取hana::type<pair<X,Y>>
。为此,我必须创建一个非常丑陋的序列,例如tuple< pair<type<int>, type<int>>, pair<type<double>, type<int>>, pair<type<float>, type<double>> >
. 正如您可以想象的那样,这可能会很快变得很糟糕,例如,如果我有一个序列tuple<int, pair<X,Y>, double, float>
等。有什么方法可以创建一个统一的方法来处理这个问题?我来自 MPL/fusion 背景,在那里我可以直接使用类型而无需包装类型。谢谢