C++ 编译时的静态数组
我想知道是否可以进行以下操作 答案更通用,从某种意义上说,数组的类型是模板化的,而不仅仅是无符号的:
我已将整个事物封装在一个结构中,如下所示:
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;
};
};
但我收到以下错误:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你缩进结构会有帮助。问题是您正在数组结构内定义数据静态成员变量。但它应该在命名空间范围内:
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:
呃...你为什么不直接使用 std::vector 呢?或者,如果您想要一个不可调整的数组,那么在 boost 中使用等效类型?
Erm... Why don't you just use std::vector? Or if you want a non-resiable array, then use the equivalent type in boost?
我不认为像“template”这样的表达式是 C++11 可变参数模板的有效用法。
据我了解,它可以有两种形式:
template:可变数量的泛型类型参数
template:可变数量的整数(非类型)参数
有关可变参数模板,请参阅这篇维基百科文章。
I don't think the expression like 'template ' is a valid usage of C++11 variadic templates.
It can be of two forms as I understand:
template <typename... args>
: variable number of generic type parameterstemplate <int... args>
: variable number of integer(non-type) parametersRefer to this wikipedia article for variadic templates.