Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 378
wimalopaan Avatar asked Dec 07 '25 08:12

wimalopaan


1 Answers

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)];
};
like image 64
Jarod42 Avatar answered Dec 12 '25 13:12

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!