Static arrays at compile time in C++

后端 未结 3 1522
囚心锁ツ
囚心锁ツ 2021-02-03 16:21

I was wondering if its possible to make the following answer more generic, in the sense that the type of the array be templated instead of just unsigned:

I\'ve enclosed

3条回答
  •  轮回少年
    2021-02-03 16:52

    It helps if you indent the struct. The problem is that you are defining the data static member variable inside the Array struct. But it should be at namespace scope:

    template
    struct Array
    {
        template struct ArrayHolder {
            static const ArrayType data[sizeof...(args)];
        };
    
        template class F, ArrayType... args> 
            struct generate_array_impl {
                typedef typename generate_array_impl::value, args...>::result result;
            };
    
        template class F, ArrayType... args> 
            struct generate_array_impl<0, F, args...> {
                typedef ArrayHolder::value, args...> result;
            };
    
        template class F> 
            struct generate_array {
                typedef typename generate_array_impl::result result;
            };
    };
    
    template template 
            const ArrayType Array::ArrayHolder::data[sizeof...(args)] = { args... };
    

提交回复
热议问题