fold后如何得到标准的mpl序列
How to obtain standard mpl sequence after fold
如果我使用boost::mpl,让我们看看下面的代码:
typedef fold<
vector<long,float,long>
, set0<>
, insert<_1,_2>
>::type s;
BOOST_MPL_ASSERT_RELATION( size<s>::value, ==, 2 );
如何再次将 s
转换为类型 t = boost::mpl::set<long,float>
,以便我可以使用 t
选择部分专用的模板函数(在 boost::mpl:: set``) 提取元素类型并将其转换为 std::tuple<long,float>
。这是别的东西
这是一个完整的例子。我调用元函数 Unify
但显然你可以随意调用它。
它的工作原理非常简单,它只是一次从输入序列中删除一个元素,然后构建一个可变列表,最后将它们转储到所需的序列类型中。
#include <boost/mpl/set.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/insert.hpp>
#include <boost/mpl/erase_key.hpp>
#include <tuple>
template <template <class...> class OutSeqType,
class Sequence,
std::size_t nSeqSize,
class ... Elements>
struct Unify
{
typedef typename boost::mpl::front<Sequence>::type Next;
typedef typename Unify<
OutSeqType,
typename boost::mpl::erase_key<Sequence, Next>::type,
nSeqSize - 1, Next, Elements...>::type type;
};
template <template <class...> class OutSeqType,
class Sequence,
class ... Elements>
struct Unify<OutSeqType, Sequence, 0ul, Elements...>
{
typedef OutSeqType<Elements...> type;
};
int main()
{
typedef boost::mpl::insert<
boost::mpl::insert<
boost::mpl::insert<
boost::mpl::set<>,
int>::type,
float>::type,
int*>::type Set;
typedef Unify<
std::tuple,
Set,
boost::mpl::size<Set>::type::value
>::type Set2;
//This compile error will print the type of Set2
Set2::asdfl;
}
出于某种原因,我不确定我是否不能在 set
上使用 pop_front
,所以我改用了 erase_key
。这将它限制为关联容器,但它可以推广到任何类型的容器,只需做更多的工作。我会把它留作练习。
如果我使用boost::mpl,让我们看看下面的代码:
typedef fold<
vector<long,float,long>
, set0<>
, insert<_1,_2>
>::type s;
BOOST_MPL_ASSERT_RELATION( size<s>::value, ==, 2 );
如何再次将 s
转换为类型 t = boost::mpl::set<long,float>
,以便我可以使用 t
选择部分专用的模板函数(在 boost::mpl:: set``) 提取元素类型并将其转换为 std::tuple<long,float>
。这是别的东西
这是一个完整的例子。我调用元函数 Unify
但显然你可以随意调用它。
它的工作原理非常简单,它只是一次从输入序列中删除一个元素,然后构建一个可变列表,最后将它们转储到所需的序列类型中。
#include <boost/mpl/set.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/insert.hpp>
#include <boost/mpl/erase_key.hpp>
#include <tuple>
template <template <class...> class OutSeqType,
class Sequence,
std::size_t nSeqSize,
class ... Elements>
struct Unify
{
typedef typename boost::mpl::front<Sequence>::type Next;
typedef typename Unify<
OutSeqType,
typename boost::mpl::erase_key<Sequence, Next>::type,
nSeqSize - 1, Next, Elements...>::type type;
};
template <template <class...> class OutSeqType,
class Sequence,
class ... Elements>
struct Unify<OutSeqType, Sequence, 0ul, Elements...>
{
typedef OutSeqType<Elements...> type;
};
int main()
{
typedef boost::mpl::insert<
boost::mpl::insert<
boost::mpl::insert<
boost::mpl::set<>,
int>::type,
float>::type,
int*>::type Set;
typedef Unify<
std::tuple,
Set,
boost::mpl::size<Set>::type::value
>::type Set2;
//This compile error will print the type of Set2
Set2::asdfl;
}
出于某种原因,我不确定我是否不能在 set
上使用 pop_front
,所以我改用了 erase_key
。这将它限制为关联容器,但它可以推广到任何类型的容器,只需做更多的工作。我会把它留作练习。