jan.sende
jan.sende

Reputation: 860

How can I unwrap a parameter pack wrapper?

I want to do calculations with std::ratio types at compile time. I already wrote a basic function working on parameter packs. However, to save the the ratios in other objects, I put it in a parameter pack wrapper. How can I unwrap the wrapped parameter pack, to shove it in my function?

My code is the following:

#include <ratio>
#include <functional>
#include <initializer_list>

namespace cx {
    // I need constexpr accumulate.
    // However, the standard library currently doesn't provide it.
    // I therefore just copied the code from
    // https://en.cppreference.com/w/cpp/algorithm/accumulate
    // and added the constexpr keyword.
    template<class InputIt, class T, class BinaryOperation>
    constexpr T accumulate(InputIt first, InputIt last, T init, BinaryOperation op)
    {
        for (; first != last; ++first) {
            init = op(std::move(init), *first); // std::move since C++20
        }
        return init;
    }
}

// wrapper type
template <class...T> struct wrapper { };

// helper to get the value out of the ratio type        
template <class T>
struct get_val {
    static constexpr auto value = double(T::num) / double(T::den);
};

// function for calculating the product of a vector type
template <class T>
constexpr auto product(T values) {
    return cx::accumulate(std::begin(values),
                          std::end(values),
                          1,
                          std::multiplies<typename T::value_type>());
}

// my calculation (needs a parameter pack, can't the handle wrapper type)
template <class...T>
struct ratio_product
{
    // this works by wrapping the Ts into an initializer list
    // and using that for the calculation
    static constexpr auto value =
        product(std::initializer_list<double>{get_val<T>::value...});
};

//test
int main() {
    //this works on a parameter pack (compiles)
    static_assert(ratio_product<
        std::ratio<5>, std::ratio<5>, std::ratio<4>
    >::value == 100,"");

    //this should work on a parameter pack wrapper (does not compile)
    static_assert(ratio_product<
        wrapper<
            std::ratio<5>, std::ratio<5>, std::ratio<4>
        >
    >::value == 100,"");
}

Upvotes: 0

Views: 686

Answers (2)

Quentin
Quentin

Reputation: 63124

Here's a reusable way to unwrap a parameter pack into an arbitrary template:

template <class Wrapper, template <class...> class Template>
struct unwrap;

template <class... Ts, template <class...> class Template>
struct unwrap<wrapper<Ts...>, Template> {
    using type = Template<Ts...>;
};

Which yields the following static_assert which does what you expect:

static_assert(
    unwrap<
        wrapper<std::ratio<5>, std::ratio<5>, std::ratio<4>>,
        ratio_product
    >::type::value == 100, "");

Upvotes: 2

jan.sende
jan.sende

Reputation: 860

I found the solution. By using a template specialization it is possible to extract the parameter pack from the wrapper:

template <class...T> struct ratio_product {
    static constexpr auto value = product(std::initializer_list<double>{get_val<T>::value...});
};
template <class...T> struct ratio_product<wrapper<T...>> {
    static constexpr auto value = ratio_product<T...>::value;
};

Upvotes: 0

Related Questions