How to swap two parameters of a variadic template at compile time?

烈酒焚心 提交于 2019-12-05 06:04:40

The link I posted on the comment is my own implementation of a std::bind()-like metafunction for metafunctions.

What I did is to transform the bind call parameters from its value (A value or a placeholder) to a value, or the value represented by that placeholder.

In your case you could try a similar approach: Map the sequence from placeholders (The values passed to swap) to the corresponding values of the sequence. Something like:

template<std::size_t I>
struct placeholder{};

using _1 = placeholder<0>;
... //More placeholder aliases

template<typename SEQ , typename... PLACEHOLDERS>
struct swap;

template<std::size_t... Is , std::size_t... Ps>
struct swap<sequence<Is...>,placeholder<Ps>...>
{
    template<typename PLACEhOLDER>
    struct transformation;

    template<std::size_t I>
    struct transformation<placeholder<I>>
    {
        static constexpr const std::size_t result = get<sequence<Is...>,I>::value;
    };

    using result = map<transformation,sequence<Is...>>;
};

Where map is a metafunction similar to std::transform() (Very easy to write), and get a metafunction which retrieves the Ith element of a sequence (Easy too).

This could be used on this way:

 using swapped = typename swap<sequence<1,2,3>,_3,_2,_1>::result; //swapped is sequence<3,2,1>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!