C++ 使用boost::hana筛选具有类型的列表
我试图筛选出一个包含类型的列表,但这似乎不起作用。我确信我在这里做错了什么,我创建了一个测试来重现它:C++ 使用boost::hana筛选具有类型的列表,c++,c++14,boost-hana,C++,C++14,Boost Hana,我试图筛选出一个包含类型的列表,但这似乎不起作用。我确信我在这里做错了什么,我创建了一个测试来重现它: #include <iostream> #include <boost/hana.hpp> #include <boost/hana/ext/std/tuple.hpp> struct X { }; struct Y { }; struct Z { }; int main(int argc, char **argv) { namespace
#include <iostream>
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>
struct X {
};
struct Y {
};
struct Z {
};
int main(int argc, char **argv) {
namespace hana = boost::hana;
constexpr std::tuple<X, Y, Z> list;
constexpr std::tuple<X> filterlist;
auto t = hana::filter(list, [&](auto t) {
return hana::not_(hana::contains(filterlist, hana::decltype_(t)));
});
std::cout << "filtered list contains " << hana::size(t) << " items, expected 2 items" << std::endl;
return 0;
}
#包括
#包括
#包括
结构X{
};
结构{
};
结构Z{
};
int main(int argc,字符**argv){
名称空间hana=boost::hana;
constexpr std::元组列表;
constexpr std::元组过滤器列表;
自动t=hana::过滤器(列表,[&](自动t){
返回hana::not_Ut(hana::contains(filterlist,hana::decltype_Ut));
});
std::cout问题在于您正在检查类型(decltype_ux{})=type{}
)在筛选器列表中,该列表包含实际对象,而不是类型。换句话说,这有点像是您试图将表示某种类型的T
对象与实际类型的T
对象进行比较,这在语义上毫无意义。相反,您需要的是以下内容:
#include <iostream>
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>
namespace hana = boost::hana;
struct X { };
struct Y { };
struct Z { };
int main(int argc, char **argv) {
constexpr std::tuple<X, Y, Z> list;
constexpr std::tuple<hana::type<X>> filterlist;
auto t = hana::remove_if(list, [&](auto t) {
return hana::contains(filterlist, hana::decltype_(t));
});
std::cout << "filtered list contains " << hana::size(t) << " items, expected 2 items" << std::endl;
}
作为最后一个注释,请注意这些构造,因为它们是O(n ^ 2)编译时间。如果需要高效的查找,请考虑使用<代码> HANA::设置< /C> >(现在执行很糟糕,但当我有更多的时间时,它会变好)。
#include <iostream>
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>
namespace hana = boost::hana;
struct X { };
struct Y { };
struct Z { };
int main(int argc, char **argv) {
constexpr std::tuple<X, Y, Z> list;
constexpr std::tuple<X> filterlist;
auto t = hana::remove_if(list, [&](auto t) {
return hana::any_of(filterlist, [&](auto u) {
return hana::decltype_(u) == hana::decltype_(t);
});
});
std::cout << "filtered list contains " << hana::size(t) << " items, expected 2 items" << std::endl;
}