samedi 2 avril 2016

Why is expanding a parameter pack into an array not a constant expression?

Yes, I can use std::initializer_list. Doesn't matter for the purposes of this question.

Question is, why is unpacking a parameter pack not a constant expression, even though I am passing only integer literals to the constructor?

template<typename T>
class ilist
{
private:
    const T* beg;
    const T* end;

public:
    template<typename... Args>
    constexpr ilist(Args&&... args) noexcept
    {
        // constexpr variable 'arr' must be initialized by a constant expression
        constexpr T arr[sizeof...(args)] = { args... };
        beg = arr;
        end = arr + sizeof...(args);
    }
};

int main()
{
    constexpr ilist<int> a = { 1, 2, 3, 4 };
}

Aucun commentaire:

Enregistrer un commentaire