为什么将参数包扩展为数组而不是常量表达式?

时间:2016-04-02 20:10:54

标签: c++ c++11 variadic-templates constexpr

是的,我可以使用std::initializer_list。对于这个问题的目的无关紧要。

问题是,为什么解包参数包而不是常量表达式,即使我只将整数文字传递给构造函数?

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 };
}

0 个答案:

没有答案