I have the following class:
template <size_t N, float... Lam> requires (sizeof...(Lam) == N)
class Spectrum {
public:
Spectrum() : wavelengths{ static_cast<float>(Lam)... }
{
values.fill(0.f);
};
template <IsNumeric T>
Spectrum(T v) : wavelengths{ static_cast<float>(Lam)... }
{
values.fill(static_cast<float>(v));
};
template <IsNumeric... T> requires (sizeof...(T) == N)
Spectrum(T... v) : wavelengths{ static_cast<float>(Lam)... }, values { static_cast<float>(v)... }
{ };
... // Other methods/overloads that aren't relevant here
private:
std::array<float, N> values;
const std::array<float, N> wavelengths;
};
I can then call this using:
Spectrum<3, 450.f, 500.f, 600.f> color{ 1,2,3 };
And it behaves as expected (the wavelengths are populated with 450.f, 500.f, 600.f
and the values are populated with 1.f, 2.f, 3.f
.
I would like to be able to allow users to instead call:
Spectrum<3, 450, 500, 600> color{1,2,3};
in this case, 450
is an int
, so I'd need it to be cast to a float
, in a similar way to what I already do in the constructor templates. I'm just not sure if a similar idea can be expressed in the class template itself.
template<auto...Lam>
using Spectrum_t = Spectrum<sizeof...(Lam), static_cast<float>(Lam)...>;
they don't even have to pass 3
. Not sure why you are passing it.
Also, storing Lam...
in const wavelengths
seems like a very strange choice.
static constexpr std::array<float, N> wavelengths() {
return {{Lam...}};
}
or an inline constexpr
static value.