jeudi 23 avril 2015

Someone explain why the ambiguity here, please?

Fill<T, Pack, Size, Value> is to be the type Pack<Value, Value, ..., Value>, where Value is repeated Size times. Can someone explain, why this is ambiguous?

template <typename T, template <T...> class Pack, int Size, int Count, typename Output, T Value>
struct FillHelper;

template <typename T, template <T...> class P, int Size, int Count, T... Output, T Value>
struct FillHelper<T, P, Size, Count, P<Output...>, Value> :
    FillHelper<T, P, Size, Count + 1, P<Output..., Value>, Value> {};

template <typename T, template <T...> class P, int Size, T... Output, T Value>
struct FillHelper<T, P, Size, Size, P<Output...>, Value> {
    using type = P<Output...>;
};

template <typename T, template <T...> class P, int Size, T Value>
using Fill = typename FillHelper<T, P, Size, 0, P<>, Value>::type;

template <int...> struct Pack;

int main() {
    using T = Fill<int, Pack, 10, 4>;
}

while this is not:

template <typename T, int Size, int Count, typename Output, T Value>
struct FillHelper;

template <typename T, template <T...> class P, int Size, int Count, T... Output, T Value>
struct FillHelper<T, Size, Count, P<Output...>, Value> :
    FillHelper<T, Size, Count + 1, P<Output..., Value>, Value> {};

template <typename T, template <T...> class P, int Size, T... Output, T Value>
struct FillHelper<T, Size, Size, P<Output...>, Value> {
    using type = P<Output...>;
};

template <typename T, template <T...> class P, int Size, T Value>
using Fill = typename FillHelper<T, Size, 0, P<>, Value>::type;

template <int...> struct Pack;

int main() {
    using T = Fill<int, Pack, 10, 4>;
}

It turns out that I indirectly noticed that the second is shorter and thus better than the first, but I was baffled why the first one would not compile. A better solution than the second code is welcomed, by the way.

Aucun commentaire:

Enregistrer un commentaire