简体   繁体   English

C ++中编译时的静态数组

[英]Static arrays at compile time in C++

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 the whole thing in a struct like so: 我已经将整个内容封装在这样的结构中:

template<typename ArrayType>
struct Array
{
template<ArrayType... args> struct ArrayHolder {
    static const ArrayType data[sizeof...(args)];
};

template<ArrayType... args> 
const ArrayType ArrayHolder<args...>::data[sizeof...(args)] = { args... };

template<size_t N, template<size_t> class F, ArrayType... args> 
struct generate_array_impl {
    typedef typename generate_array_impl<N-1, F, F<N>::value, args...>::result result;
};

template<template<size_t> class F, ArrayType... args> 
struct generate_array_impl<0, F, args...> {
    typedef ArrayHolder<F<0>::value, args...> result;
};

template<size_t N, template<size_t> class F> 
struct generate_array {
    typedef typename generate_array_impl<N-1, F>::result result;
};
};

but I get the following errors: 但出现以下错误:

c++-4.6 -std=c++0x -o test test.cpp
test.cpp:49:17: error: specializing member ‘Array<ArrayType>::ArrayHolder<args>::data’ requires ‘template<>’ syntax

It helps if you indent the struct. 如果缩进该结构,则有帮助。 The problem is that you are defining the data static member variable inside the Array struct. 问题是您要在Array结构中定义数据静态成员变量。 But it should be at namespace scope: 但是它应该在名称空间范围内:

template<typename ArrayType>
struct Array
{
    template<ArrayType... args> struct ArrayHolder {
        static const ArrayType data[sizeof...(args)];
    };

    template<size_t N, template<size_t> class F, ArrayType... args> 
        struct generate_array_impl {
            typedef typename generate_array_impl<N-1, F, F<N>::value, args...>::result result;
        };

    template<template<size_t> class F, ArrayType... args> 
        struct generate_array_impl<0, F, args...> {
            typedef ArrayHolder<F<0>::value, args...> result;
        };

    template<size_t N, template<size_t> class F> 
        struct generate_array {
            typedef typename generate_array_impl<N-1, F>::result result;
        };
};

template<typename ArrayType> template<ArrayType... args> 
        const ArrayType Array<ArrayType>::ArrayHolder<args...>::data[sizeof...(args)] = { args... };

Erm... Why don't you just use std::vector? Erm ...为什么不只使用std :: vector? Or if you want a non-resiable array, then use the equivalent type in boost? 或者,如果您要使用非固定数组,请在boost中使用等效类型?

I don't think the expression like 'template ' is a valid usage of C++11 variadic templates. 我不认为像“模板”这样的表达是C ++ 11可变参数模板的有效用法。

It can be of two forms as I understand: 据我了解,它可以有两种形式:

  1. template <typename... args> : variable number of generic type parameters template <typename... args> :泛型类型参数的可变数量
  2. template <int... args> : variable number of integer(non-type) parameters template <int... args> :整数(非类型)参数的可变数量

Refer to this wikipedia article for variadic templates. 有关可变参数模板,请参阅此Wikipedia文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM