Reputation: 14953
Given a template
template <int... Ints>
struct FixedArray;
How to implement a meta function that multiplies each integer value by a given number?
template <int A, typename F>
struct Mul;
Mul<2, FixedArray<5, 7, 8>>::type
is the same as
FixedArray<10, 14, 16>
Upvotes: 1
Views: 224
Reputation: 6647
To do this, you can first define an empty Mul
class:
template<int A, typename T>
struct Mul;
Then create a specialization for FixedArray
:
template<int A, int ... Ints>
struct Mul<A, FixedArray<Ints...>>
{
using type = FixedArray<(Ints * A)...>;
};
However, I'd rather have a typedef inside FixedArray
, so the resulting syntax looks more natural to me:
template <int ... Ints>
struct FixedArray
{
template<int A>
using Mul = FixedArray<(Ints * A)...>;
};
static_assert(std::same_as<FixedArray<1,2,3>::Mul<2>, FixedArray<2,4,6>>);
Upvotes: 3