I'm trying to swap two parameters of a variadic template at compile time :
template<int...Numbers>struct sequence{};
template<size_t first,size_t second>
struct Swap_Pair
{
const static size_t First = first;
const static size_t Second = second;
};
template <int...Numbers,class swap_pair>
struct Swap_Data
{
static std::array<int, sizeof...(Numbers)> data_;//How to swap Numbers base on the pair and store it in data_ ?
};
The use case should be :
sequence<1, 2, 3, 4, 5, 6> array;
auto result = Swap_Data < array, Swap_Pair<2, 5> > ::data_;
//result is now std::array which contains 1 2 6 4 5 3
I can't figure out what is the proper way of writing Swap_Data
.
And how can I make a recursive swap for swapping variadic parameters and converting into std::array at compile time ?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With