我想写一元函数,其(在Haskell)看起来大致是:升压MPL序列模式匹配
gather :: [a] -> [a] -> ([a], [a])
gather (x:xs) (_:_:ys) = <something using x, xs, and ys>
...other pattern matches...
我能够做到这一点使用卷我,自己自己的可变参数模板序列,但可以似乎不知道如何使用mpl来做到这一点。
为简单起见,我想这个样本函数(应该帮助我明白我需要什么):
//get_first :: [a] -> a
template<class SEQ_C>
get_first {
enum { value = -1 };
typedef get_first<SEQ_C> type;
};
//get_first (x:xs) = x
template<template<class T, T... S> class SEQ_C, class T, T x, T... xs>
struct get_first<SEQ_C<T, x, xs...>> {
enum { value = x };
typedef get_first<SEQ_C<T, x, xs...>> type;
};
...
typedef boost::mpl::vector_c<int 1, 2, 3> listA;
typedef get_first<listA>::type first;
std::cout << first::value << std::endl;
输出-1。
我已经尝试了很多不同的方式来获得比赛在这一点上我只是在黑暗中刺伤。该文档使其看起来像mpl::vector_c<int, x>
实际上是integral_c<int, x>
的列表 - 但尝试使用此结果是其他错误。
也许模式匹配
据我可以告诉问题是boost :: mpl序列实际上并不使用可变参数模板,而是模拟它们。所以'mpl :: vector_c'变成'mpl :: vector '重复'2147483647l'直到达到BOOST_MPL_LIMIT_VECTOR_SIZE。在我自己的roll中,我实际上使用了'template ',它告诉编译器期望什么。我想这是不符合标准的,试图弄清楚事情是如何工作的:如果你传递一个非可变模板类型作为模板参数,它应该匹配吗? –
nickdmax