Q:

Permute the parameters in a parameter pack

I have two structures
template <int ... values>
struct foo {}

template <int ... values>
struct lists {} 

I want to have a function bar that takes the lists and foo and permutes parameters of foo to return another foo
another_foo bar(lists<0,3,1,2>, foo<4,5,6,7>)

I want the type of another_foo to be foo<4,7,5,6> so basically sort one parameter in terms of another.  I also want the function to work with arbitrary parameters in list not necessarily as index to foo parameters, such as
another_foo bar(lists<20,12,21,13>, foo<4,5,6,7>)

I want another_foo to be type alias to foo<6,4,7,5>.  In this case, lists<20,12,21,13> is basically another version of lists<2,0,3,1> as in terms of ascendency that is how the elements are arranged.  How can I do this? 
Thanks

A:

template<size_t N>
constexpr size_t count_less(const int (&seq)[N], int i, size_t cur = 0) {
    return cur == N ?  0
                    : (count_less(seq, i, cur + 1) + (seq[cur] < i ?  1 : 0));
}

template<class List, class Foo, class Seq>
struct meow;

template<int... ls, int... fs, size_t... ss>
struct meow<lists<ls...>, foo<fs...>, std::index_sequence<ss...>>{
    constexpr static int lst[] = { ls... };
    constexpr static int fvals[] = {fs...};
    using type = foo<fvals[count_less(lst, lst[ss])]...>;
};

template<int... ls, int... fs>
auto bar(lists<ls...>, foo<fs...>)
    -> typename meow<lists<ls...>, foo<fs...>, 
                     std::make_index_sequence<sizeof...(ls)>>::type;

