如何从enum模板参数推断数组大小?

[英]How to deduce array size from an enum template argument?


How should I change the code below so that Array<Index> array; is enough and the SIZE is automatically deduced from the enum?
Even if the enum changes, it is guaranteed that it contains SIZE referring to the correct size.

如何修改下面的代码,使数组 数组;是否足够并且尺寸是自动从枚举中推断出来的?即使enum改变了,它也保证包含引用正确大小的大小。

template <typename Enum, int N>
class Array {

public:
    int& operator[](Enum index) { return array[index]; }

private:
    int array[N];
};

enum Index { X, Y, SIZE };

int main() {

    Array<Index, SIZE> array;

    array[X] = 1;

    return 0;
}

UPDATE: As for "Array<type> means you're creating an array of Type objects" (Jerry) and "the name of class template is a bit misleading" (Nawaz): actually I am creating CustomSqlQueryModel<TableColumns>. The above is just a simplified code, nothing more. Jerry and Nawaz are rigth: this simplified code is unfortunate.

更新:至于“Array 表示您正在创建一个类型对象数组”(Jerry)和“类模板的名称有点误导”(Nawaz):实际上我正在创建CustomSqlQueryModel 。上面只是一个简化的代码,仅此而已。Jerry和Nawaz是严格的:这种简化的代码是不幸的。

3 个解决方案

#1


6  

You can write a traits class. This requires a bit of extra work each time you define a new enum type, but no extra work for each occurrence of Array<Index> in user code:

你可以写一个特质类。这需要每次定义一个新的enum类型时都需要额外的工作,但是对于在用户代码中每次出现的数组 都没有额外的工作:

template<class Enum>
struct ArrayTraits;

template<class Enum>
struct Array {
  int& operator[](Enum index) { return array[index]; }

private:
  int array[ArrayTraits<Enum>::size];
};

enum Index { X, Y, SIZE };

template<>
struct ArrayTraits<Index> {
  enum { size = SIZE };
};


int main() {
  Array<Index> array;
  array[X] = 1;
  return 0;
}

One of the advantages of this is you can specialize the traits for external enums you don't control, as long as you know how to get the max size.

这样做的一个好处是,只要你知道如何获得最大的尺寸,你就可以为你无法控制的外部环境专门设置特征。

#2


1  

As stated, I don't think you can. If, however, you change it to something like:

如前所述,我认为你做不到。但是,如果您将其更改为以下内容:

struct Index { 
    enum { X, Y, SIZE};
};

Then your template could be something like:

那么你的模板可以是这样的:

template <class Enum>
class Array { 
// ...

private:
    int array[Enum::SIZE];
};

...and if the type you pass as Enum doesn't include some positive constant named SIZE,the instantiation won't compile. For the purpose at hand, you'd really kind of prefer that Index was a namespace, but since a namespace isn't a type, I don't think you can use it as a template argument.

…如果作为Enum传递的类型不包含一些名为SIZE的正常量,那么实例化将不会编译。就目前的目的而言,您确实希望索引是一个名称空间,但是由于名称空间不是一个类型,所以我认为您不能将它用作模板参数。

I should add, however, that I'm not sure I like this idea at all -- most people are going to think Array<type> means you're creating an array of Type objects, and this is clearly something entirely different from that...

然而,我应该补充一点,我不确定我是否喜欢这个想法——大多数人会认为数组 意味着你正在创建一个类型对象数组,而这显然与那个完全不同……

#3


0  

If you want only the size to be template argument, not the type , as from your example it seems that the type of the array would be always int, then why don't you implement this:

如果您只想要大小为模板参数,而不是类型,从您的示例来看,数组的类型似乎总是int型的,那么为什么不实现这个呢:

template <int size>
class Array {

public:
    int& operator[](int index) { return array[index]; }

    //Note this addition!
    int operator[](int index) const { return array[index]; }
private:
    int array[size];
};

int main() {

    Array<10> array;

    array[0] = 1;
    array[1] = 2;

    return 0;
}

Note this addition: it's better if you implement const version of operator[] too, so that const Array<> can use it to access the array elements, otherwise your class wouldn't work for const Array<>.

注意这个添加:如果您也实现了操作符[]的const版本,那么const数组<>可以使用它来访问数组元素,否则您的类对const数组<>将不起作用。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/02/15/7201ffa9314ee335eaebd1d67cc352.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告