我试图创建一个包含给定类型列表排列的列表。
下面的代码似乎可以运行,但是当我使用指定的列表而不是通过从实际输入中删除来生成新列表时,它没有产生预期的结果。这可以通过下面的permutation_helper和broken_helper之间的差异来证明。
有人知道为什么mpl::remove在这种情况下似乎不能按预期工作吗?
使用 permutation_helper 的输出是一个实际的列表:
下面的代码似乎可以运行,但是当我使用指定的列表而不是通过从实际输入中删除来生成新列表时,它没有产生预期的结果。这可以通过下面的permutation_helper和broken_helper之间的差异来证明。
有人知道为什么mpl::remove在这种情况下似乎不能按预期工作吗?
#include <boost/mpl/list.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/mpl/joint_view.hpp>
#include <boost/mpl/remove.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/equal.hpp>
namespace mpl = boost::mpl;
struct test_type1 {};
struct test_type2 {};
struct test_type3 {};
template< typename T >
struct permutations;
template <typename value>
struct permutations<mpl::list1< value > >: mpl::list1<mpl::list1< value > > {};
template< typename value, typename T>
struct permutation_helper:
mpl::transform< typename permutations<
mpl::list1<test_type3> >::type,
mpl::push_front< mpl::_1, value> > { };
template< typename value, typename T>
struct broken_helper:
mpl::transform< typename permutations<
mpl::remove<T, value> >::type,
mpl::push_front< mpl::_1, value> > { };
template< typename T >
struct permutations:
mpl::fold< T,
mpl::list0<>,
mpl::joint_view< mpl::_1,
broken_helper<mpl::_2, T > > > { };
typedef mpl::list2<test_type1, test_type2> typelist;
typedef permutations<typelist>::type perms;
int main() {
BOOST_MPL_ASSERT(( mpl::equal< perms, typelist > ));
return 0;
}
我使用assert来确定函数返回的内容,但是typelist并非预期结果。断言会返回以下有关broken_helper的消息:
testcase.cpp: In function ‘int main()’:
testcase.cpp:45: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::equal<boost::mpl::joint_view<boost::mpl::joint_view<boost::mpl::list0<mpl_::na>, boost::mpl::l_end>, boost::mpl::l_end>, boost::mpl::list2<test_type1, test_type2>, boost::is_same<mpl_::arg<-0x00000000000000001>, mpl_::arg<-0x00000000000000001> > >::************)’
使用 permutation_helper 的输出是一个实际的列表:
testcase.cpp: In function ‘int main()’:
testcase.cpp:45: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::equal<boost::mpl::list2<test_type1, test_type2>, boost::mpl::joint_view<boost::mpl::joint_view<boost::mpl::list0<mpl_::na>, boost::mpl::l_item<mpl_::long_<1l>, boost::mpl::l_item<mpl_::long_<2l>, test_type1, boost::mpl::list1<test_type3> >, boost::mpl::l_end> >, boost::mpl::l_item<mpl_::long_<1l>, boost::mpl::l_item<mpl_::long_<2l>, test_type2, boost::mpl::list1<test_type3> >, boost::mpl::l_end> >, boost::is_same<mpl_::arg<-0x00000000000000001>, mpl_::arg<-0x00000000000000001> > >::************)’