vendredi 30 décembre 2016

How to static_assert that an initializer list is a certain size

Would it be possible to verify that an initializer list that is being passed to a constexpr constructor is a certain size? Or would this only be possible to do at runtime?

This is the idea, but it doesn't work:

struct group
{
        constexpr group(
            std::initializer_list<std::initializer_list<UINT const> const> groups
        )
        {
            static_assert(each_list_size_greater_than_1(groups.begin(), groups.end()));
        }

        constexpr static bool each_list_size_greater_than_1(
            std::initializer_list<std::initializer_list<UINT const> const>::const_iterator const begin
            , std::initializer_list<std::initializer_list<UINT const> const>::const_iterator const end)
        {
            return begin == end || begin->size() > 1 && each_list_size_greater_than_1(begin + 1, end);
        }
};

I've looked at VS2015's std::initializer_list implementation and begin(), end() and size() are all constexpr functions.

Aucun commentaire:

Enregistrer un commentaire