在 c++11 中实现元函数 zip
我实际上想看看是否可以获得一个最小的库,该库支持我从 boost::fusion 使用的极少数操作。
这是我到目前为止所拥有的...
template < typename... Types >
struct typelist
{
};
template < template < typename... > class F, typename... Args >
struct apply
{
typedef typename F < Args... >::type type;
};
template < typename, template < typename... > class >
struct foreach;
template < typename... Types, template < typename Arg > class F >
struct foreach < typelist < Types... >, F >
{
typedef typelist < typename apply < F, Types >::type... > type;
};
由于元函数 foreach
实现很简单,我认为 zip
也会很容易。显然,情况并非如此。
template < typename... >
struct zip;
template < typename... Types0, typename... Types1 >
struct zip < typelist < Types0... >, typelist < Types1... > >
{
typedef typelist < typelist < Types0, Types1 >... > type;
};
如何将此 zip 元函数推广到任意数量的类型列表?这里我们需要的似乎是一个参数包的参数包。我不知道该怎么做。
编辑1:
is_equal
的实现...
template < std::size_t... Nn >
struct is_equal;
template < std::size_t N0, std::size_t N1, std::size_t... Nn >
struct is_equal < N0, N1, Nn... >
: and_ <
typename is_equal < N0, N1 >::type
, typename is_equal < N1, Nn... >::type
>::type
{
};
template < std::size_t M, std::size_t N >
struct is_equal < M, N > : std::false_type
{
typedef std::false_type type;
};
template < std::size_t N >
struct is_equal < N, N > : std::true_type
{
typedef std::true_type type;
};
我认为也可以对zip
采取类似的方法...还没有尝试过zip
> 还没有,但是当我回家时会这样做。
编辑2:
这是我最终认为看起来更优雅的。这基本上是 Vaughn Cato 方法的变体。
namespace impl
{
template < typename Initial, template < typename, typename > class F, typename... Types >
struct foldl;
template < typename Initial, template < typename, typename > class F, typename First, typename... Rest >
struct foldl < Initial, F, First, Rest... >
{
typedef typename foldl < typename F < Initial, First >::type, F, Rest... >::type type;
};
template < typename Final, template < typename, typename > class F >
struct foldl < Final, F >
{
typedef Final type;
};
template < typename Type, typename TypeList >
struct cons;
template < typename Type, typename... Types >
struct cons < Type, typelist < Types... > >
{
typedef typelist < Types..., Type > type;
};
template < typename, typename >
struct zip_accumulator;
template < typename... Types0, typename... Types1 >
struct zip_accumulator < typelist < Types0... >, typelist < Types1... > >
{
typedef typelist < typename cons < Types1, Types0 >::type... > type;
};
template < typename... Types0 >
struct zip_accumulator < typelist <>, typelist < Types0... > >
{
typedef typelist < typelist < Types0 >... > type;
};
template < typename... TypeLists >
struct zip
{
typedef typename foldl < typelist <>, zip_accumulator, TypeLists... >::type type;
};
}
template < typename... TypeLists >
struct zip
{
static_assert(and_ < typename is_type_list < TypeLists >... >::value, "All parameters must be type lists for zip");
static_assert(is_equal < TypeLists::length... >::value, "Length of all parameter type lists must be same for zip");
typedef typename impl::zip < TypeLists... >::type type;
};
template < typename... TypeLists >
struct zip < typelist < TypeLists... > > : zip < TypeLists... >
{
};
这将 zip
视为 fold
操作。
I am actually trying to see if I can get a minimal library that supports the very few operations I use from boost::fusion.
Here is what I have so far...
template < typename... Types >
struct typelist
{
};
template < template < typename... > class F, typename... Args >
struct apply
{
typedef typename F < Args... >::type type;
};
template < typename, template < typename... > class >
struct foreach;
template < typename... Types, template < typename Arg > class F >
struct foreach < typelist < Types... >, F >
{
typedef typelist < typename apply < F, Types >::type... > type;
};
Since the meta-function foreach
implementation is trivial, I thought zip
would be easy too. Apparently, this is not the case.
template < typename... >
struct zip;
template < typename... Types0, typename... Types1 >
struct zip < typelist < Types0... >, typelist < Types1... > >
{
typedef typelist < typelist < Types0, Types1 >... > type;
};
How can I generalize this zip
meta-function to arbitrary number of typelists? What we need here seems to be a parameter pack of parameter packs. I am not sure how to do that.
Edit 1:
Implementation of is_equal
...
template < std::size_t... Nn >
struct is_equal;
template < std::size_t N0, std::size_t N1, std::size_t... Nn >
struct is_equal < N0, N1, Nn... >
: and_ <
typename is_equal < N0, N1 >::type
, typename is_equal < N1, Nn... >::type
>::type
{
};
template < std::size_t M, std::size_t N >
struct is_equal < M, N > : std::false_type
{
typedef std::false_type type;
};
template < std::size_t N >
struct is_equal < N, N > : std::true_type
{
typedef std::true_type type;
};
A similar approach can be taken to zip
as well I think... haven't tried with zip
yet, but will do so when I get back home.
Edit 2:
Here is what I finally thought looked more elegant. This is basically a variation of Vaughn Cato's approach.
namespace impl
{
template < typename Initial, template < typename, typename > class F, typename... Types >
struct foldl;
template < typename Initial, template < typename, typename > class F, typename First, typename... Rest >
struct foldl < Initial, F, First, Rest... >
{
typedef typename foldl < typename F < Initial, First >::type, F, Rest... >::type type;
};
template < typename Final, template < typename, typename > class F >
struct foldl < Final, F >
{
typedef Final type;
};
template < typename Type, typename TypeList >
struct cons;
template < typename Type, typename... Types >
struct cons < Type, typelist < Types... > >
{
typedef typelist < Types..., Type > type;
};
template < typename, typename >
struct zip_accumulator;
template < typename... Types0, typename... Types1 >
struct zip_accumulator < typelist < Types0... >, typelist < Types1... > >
{
typedef typelist < typename cons < Types1, Types0 >::type... > type;
};
template < typename... Types0 >
struct zip_accumulator < typelist <>, typelist < Types0... > >
{
typedef typelist < typelist < Types0 >... > type;
};
template < typename... TypeLists >
struct zip
{
typedef typename foldl < typelist <>, zip_accumulator, TypeLists... >::type type;
};
}
template < typename... TypeLists >
struct zip
{
static_assert(and_ < typename is_type_list < TypeLists >... >::value, "All parameters must be type lists for zip");
static_assert(is_equal < TypeLists::length... >::value, "Length of all parameter type lists must be same for zip");
typedef typename impl::zip < TypeLists... >::type type;
};
template < typename... TypeLists >
struct zip < typelist < TypeLists... > > : zip < TypeLists... >
{
};
This treats zip
as a fold
operation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我发现的最短的实现:
This is the shortest implementation I've discovered:
似乎可以使用成熟的列表(这意味着头、尾和反操作)和递归。使用 GCC 4.7 的快照进行测试,所有
std
内容均来自
:您可能希望向所有这些添加错误检查(我正在考虑无效当前仅保留为不完整类型的实例化)。坦率地说,考虑到我花了很多时间才弄清楚这一点,我建议坚持使用 Boost.MPL。像惰性评估这样的事情(我不需要做 SFINAE 的事情)是一个福音,我不喜欢重新发明它们。再加上 C++11 的出现,您将能够两全其美。
忘了说Boost.MPL还具有通用性的优点。它可以适用于满足其序列概念之一的任何类型(也可以非侵入性地适应预先存在的类型),而您则强制使用
typelist
。Seems to be doable with fully-fledged lists (that means head, tail and cons operations) and recursion. Tested with a snapshot of GCC 4.7, all the
std
stuff is from<type_traits>
:You might want to add error checking to all this (I'm thinking of invalid instantiations that are currently simply left as incomplete types). Frankly given the amount of time it took me to figure this out, I'd suggest sticking with Boost.MPL. Things like lazy evaluation (with which I wouldn't have needed to do the SFINAE stuff) are a boon and I'd don't like reinventing them. Plus the day it's C++11 enabled you reap the best of both worlds.
I forgot to mention that Boost.MPL also has the advantages of genericity. It can work on any type that satisfies one of its sequence concept (it's also possible to non-intrusively adapt a preexisting type), whereas you force the use of
typelist
.