如何從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數組<>將不起作用。


注意!

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



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