OiO.lk Blog templates Parameter pack to initialize member std::array
templates

Parameter pack to initialize member std::array


I have a class Color which is a thin wrapper around std::array. I would like to be able to use it in 2 ways:

  1. Color<5> color{1,2,3,4,5} should produce color.values = [1,2,3,4,5]
  2. Color<5> color{3} should produce color.values = [3,3,3,3,3]

What I currently have is:

template <size_t N>
class Color {
public:
    Color() = default;
    Color(std::array<float, N> values) : values{ values } {};

    template <IsNumeric... T>
    Color(T... v) : values{ static_cast<float>(v)... } { };

    ....
}

This works correctly for my first-case. However for the second case it only produces: color.values = [3,0,0,0,0]. I am at a loss for how to get the second case to work. I have tried:

template <size_t N>
class Color {
public:
    Color() = default;
    Color(std::array<float, N> values) : values{ values } {};

    template <IsNumeric... T, typename std::enable_if<(sizeof...(T) == N), bool>::type = true>
    Color(T... v) : values{ static_cast<float>(v)... } { };

    template <IsNumeric T>
    Color(T v) : values{ std::array<float, N>{ static_cast<float>(v) } } { };

    ...
}

But this does not change anything.



You need to sign in to view this answers

Exit mobile version