mardi 2 janvier 2018

How to constrain a parameter type to allow only std::initializer_list

I would like to have only one template function. So I came up with like...

template<typename Iteratable, size_t N,
         typename = 
            std::enable_if_t<
                std::is_same_v<Iteratable, const std::initializer_list<size_t> > ||
                std::is_same_v<Iteratable, const std::array<size_t, N > >
            > 
>
std::ostream& operator << (std::ostream& os, Iteratable& in) {
    std::copy(std::begin(in), std::end(in), std::ostream_iterator<size_t>(os, " ") ); 
    return os;
}

It seems because of N in std::array<size_t, N>, the specialization fails.

Is there any how not to write 2 function for this use case?

Aucun commentaire:

Enregistrer un commentaire