Unexpected non-constant std::initializer_list

2019-02-09 13:54发布

问题:

I was toying a little bit with the indices trick to see where I could go to with and came across a strange error... First, the plain not-so-old indices:

template<std::size_t...>
struct indices {};

template<std::size_t N, std::size_t... Indices>
struct make_indices:
    make_indices<N-1, N-1, Indices...>
{};

template<std::size_t... Indices>
struct make_indices<0, Indices...>:
    indices<Indices...>
{};

I created a compile-time array class derived from a std::initializer_list and had it indexable (assume that N3471 is support by your compiler. It will be in the next standard anyway). Here it is:

template<typename T>
struct array:
    public std::initializer_list<T>
{
    constexpr array(std::initializer_list<T> values):
        std::initializer_list<T>(values)
    {}

    constexpr auto operator[](std::size_t n)
        -> T
    {
        return this->begin()[n];
    }
};

So, I tried to create a function that returns a copy of an array after having added 1 to each of its members:

template<typename T, std::size_t... I>
auto constexpr add_one(const array<T>& a, indices<I...>)
    -> const array<T>
{
    return { (a[I]+1)... };
}

And to finish with the code, here is my main:

int main()
{
    constexpr array<int> a = { 1, 2, 3 };
    constexpr auto b = add_one(a, make_indices<a.size()>());

    return 0;
}

I did not think that code would compile anyway, but I am quite surprised by the error message (Here is the ideone code):

In function 'int main()':
error: 'const smath::array<int>{std::initializer_list<int>{((const int*)(& const int [3]{2, 3, 4})), 3u}}' is not a constant expression

So, could someone explain to me what exactly is not constant enough for the compiler in the above code?

EDIT: Follow-ups for that question

  • Is it legal to declare a constexpr std::initializer_list object?
  • Confusion about constant expression

回答1:

From: the man himself http://www.stroustrup.com/sac10-constexpr.pdf

Specifically: its return type, and the types of its parameters (if any), are literal types (see x2.2). For concreteness, literal types include bool, int, or double; its body is a compound statement of the form { return expr; } where expr is such that if arbitrary constant expressions of appropriate types are substituted for the parameters in expr, then the resulting expression is a constant expression as defined in introductory paragraph of x2. The expression expr is called a potential constant expression.