簡體   English   中英

使用轉換后的`boost :: mpl :: vector`s標簽分配

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

我正在嘗試使用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演示

如您所見,目標是在initializedestroy過程中擁有兩個可以訪問Stuff包的進程。 但是,正如這里回答的那樣, 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) {
     ^
  • 如何輕松地雙向操作類型列表?
  • Boost.MPL本質上與可變參數模板不兼容嗎?

我可以拋棄Boost.MPL(如果需要的話),只要它是標准或Boost。 我正在使用MSVC 14.1。

Boost.MPL本質上與可變參數模板不兼容嗎?

基本上。 MPL早於C ++ 11,因此要使用MPL,您需要使用其算法-因此需要將Sequence概念與Iterators等配合使用。幾乎可以肯定有一種非常簡短,聰明的方法,但是我只能找到那些與猜測和檢查。


至少,如果您需要做的只是逆向操作,則可以在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;

因此給出:

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

然后reverse<InitOrder>將是typelist<struct Baz, struct Bar, struct Foo> ,因此可以按您想要的方式使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM