The result that i am trying to achieve is:
removeduplicates<TMPArray<8, 8, 9, 9, 10, 11, 11>>::type
results in the same type as TMPArray<8, 9, 10, 11>
So far my solution looks like this:
template<int... I>
struct TMPArray {};
template <typename Arr1, typename Arr2>
struct concat;
template <int Ts, int... Us>
struct concat<TMPArray<Ts>, TMPArray<Us...>>
{
using type = TMPArray<Ts, Us...>;
};
template <int... Ts, int... Us>
struct concat<TMPArray<Ts...>, TMPArray<Us...>>
{
using type = TMPArray<Ts..., Us...>;
};
template<typename TMPArray>
struct removeduplicates;
template<bool isSame, typename TMPArray>
struct makeremoveduplicates;
template<int Head, int Sec, int... Tail>
struct removeduplicates<TMPArray<Head, Sec, Tail...>> {
using type = makeremoveduplicates<Head == Sec, TMPArray<Head, Sec, Tail...>>;
};
template<int Head, int Sec, int Third, int... Tail>
struct makeremoveduplicates<false, TMPArray<Head, Sec, Third, Tail...>> {
using type = concat<TMPArray<Head>,makeremoveduplicates<Sec == Third,TMPArray<Sec, Third, Tail... >>::type>::type;
};
template<int Head, int Sec, int Third, int... Tail>
struct makeremoveduplicates<true, TMPArray<Head, Sec, Third, Tail...>> {
using type = makeremoveduplicates<Sec == Third, TMPArray<Sec, Third, Tail... >>::type;
};
template<int Head, int Sec>
struct makeremoveduplicates<true, TMPArray<Head, Sec>> {
using type = TMPArray<Head>;
};
template<int Head, int Sec>
struct makeremoveduplicates<false, TMPArray<Head, Sec>> {
using type = TMPArray<Head, Sec>;
};
The idea behind this solution is that i am using the helper makeremoveduplicates to compare the first two elements and call the specialised template for when they match or don't match. This will append the result recursively using the concat metafunction.
I encounter a compilation error which states that:
Error C2923 'concat': 'makeremoveduplicates<Sec==Third,TMPArray<Sec,Third,Tail...>>::type' is not a valid template type argument for parameter 'Arr2'
I would expect makeremoveduplicates type to evaluate to TMPArray, as per the base case:
template<int Head, int Sec>
struct makeremoveduplicates<true/false, TMPArray<Head, Sec>>
and the result of concat:
template <int Ts, int... Us>
struct concat<TMPArray<Ts>, TMPArray<Us...>>
{
using type = TMPArray<Ts, Us...>;
};
Why is this not a valid type?