wimalopaan
wimalopaan

Reputation: 5472

Compile-time reversal of parameter-pack expansion

In the following class template I want to initalize the member-array b with the values of the parameter-pack vv in reverse-oder!

template<typename T>
struct Test {
public:
    template<typename... B>
    explicit Test(B... vv) : b{vv...,} // how to initialize member b with pack expansion v... in reverse order
    {
    }
private:
    std::byte b[sizeof(T)];
};

I can't imagine how to reverse the parameter-pack expansion in a form, that it is as well usable in an initializer list.

Upvotes: 3

Views: 1003

Answers (2)

Massimiliano Janes
Massimiliano Janes

Reputation: 5624

given that b is a non static data member, the following more compact version may turn out equivalent once optimized(*):

template<typename T>
struct Test {
public:
    template<typename... B>
    explicit Test(B... vv){ auto rb = std::rbegin(b); ((*rb++ = vv),...); }
private:
    char b[sizeof(T)];
};

(*) unless, Jarod42's solution ends up with the already reversed B's in data section when constexpr...

EDIT: it looks like both trunk clang and gcc are so smart to place the reversed B's in the data section both in Jarod42's code and mine !

Upvotes: 0

Jarod42
Jarod42

Reputation: 218278

With delegating constructor, you may do something like:

template<typename T>
struct Test {
    template <std::size_t...Is, typename... B>
    Test(std::index_sequence<Is...>, B&&... vv) :
        b{std::get<sizeof...(Is) - 1 - Is>(std::tie(vv...))...}
    {}

public:
    template<typename... B>
    explicit Test(B... vv) : Test(std::index_sequence_for<B...>{}, vv...) {}
private:
    std::byte b[sizeof(T)];
};

Upvotes: 5

Related Questions