简体   繁体   English

使用转换后的`boost :: mpl :: vector`s标签分配

[英]Tag dispatching with transformed `boost::mpl::vector`s

I am trying to tag-dispatch into a function with a reversed copy of a boost::mpl::vector : 我正在尝试使用boost::mpl::vector的反向副本将标签分派为一个函数:

using InitOrder = boost::mpl::vector<
    struct Foo,
    struct Bar,
    struct Baz
>;

template <class... Stuff>
void initialize(boost::mpl::vector<Stuff...>) {
    // Initialize in-order
}

template <class... Stuff>
void destroy(boost::mpl::vector<Stuff...>) {
    // Exit in-order
}

void initializeAll() {
    initialize(InitOrder{});
}

void destroyAll() {
    destroy(typename boost::mpl::reverse<InitOrder>::type{});
}

Coliru demo Coliru演示

As you can see, the goal is to have two processes in initialize and destroy that have access to the Stuff pack. 如您所见,目标是在initializedestroy过程中拥有两个可以访问Stuff包的进程。 However, as answered here , boost::mpl::reverse<InitOrder>::type is actually not a boost::mpl::vector , and the dispatching fails: 但是,正如这里回答的那样, boost::mpl::reverse<InitOrder>::type实际上不是 boost::mpl::vector ,并且调度失败:

main.cpp:27:2: error: no matching function for call to 'destroy'
        destroy(typename boost::mpl::reverse::type{});
        ^~~~~~~
main.cpp:18:6: note: candidate template ignored: could not match 'vector' against 'v_item'
void destroy(boost::mpl::vector) {
     ^
  • How can I operate on a type list in both directions easily? 如何轻松地双向操作类型列表?
  • Is Boost.MPL inherently incompatible with variadic templates? Boost.MPL本质上与可变参数模板不兼容吗?

I can ditch Boost.MPL if needed, provided the alternative is standard or Boost. 我可以抛弃Boost.MPL(如果需要的话),只要它是标准或Boost。 I'm using MSVC 14.1. 我正在使用MSVC 14.1。

Is Boost.MPL inherently incompatible with variadic templates? Boost.MPL本质上与可变参数模板不兼容吗?

Basically. 基本上。 MPL predates C++11, so to use MPL, you need to use their algorithms - so their Sequence concept with their Iterators, etc. There's almost certainly a really short, clever way to do this, but I can only ever find those out with guess and check. MPL早于C ++ 11,因此要使用MPL,您需要使用其算法-因此需要将Sequence概念与Iterators等配合使用。几乎可以肯定有一种非常简短,聪明的方法,但是我只能找到那些与猜测和检查。


At least, if all you need to do is reverse, this is straightforward to implement in C++11: 至少,如果您需要做的只是逆向操作,则可以在C ++ 11中直接实现:

template <typename...> struct typelist { };

template <typename TL, typeanme R>
struct reverse_impl;

template <typename T, typename... Ts, typename... Us>
struct reverse_impl<typelist<T, Ts...>, typelist<Us...>>
: reverse_impl<typelist<Ts...>, typelist<Us..., T>>
{ };

template <typename... Us>
struct reverse_impl<typelist<>, typelist<Us...>>
{
    using type = typelist<Us...>;
};

template <typename TL>
using reverse = typename reverse_impl<TL, typelist<>>::type;

So given: 因此给出:

using InitOrder = typelist<struct Foo, struct Bar, struct Baz>;

Then reverse<InitOrder> would be typelist<struct Baz, struct Bar, struct Foo> , and so would be usable in the way you'd want. 然后reverse<InitOrder>将是typelist<struct Baz, struct Bar, struct Foo> ,因此可以按您想要的方式使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM