将数组作为模板类型传递

pyt*_*nic 7 c++ templates

我需要传递一个数组作为模板类型.怎么能实现它.例如,我想要这样的东西.

Fifo<array, Q_SIZE> f; // This is a queue of arrays (to avoid false sharing)
Run Code Online (Sandbox Code Playgroud)

我该怎么代替阵列?假设我需要一个int数组.另请注意,我不想要std::vector或指向数组的指针.我想要整个基本数组,相当于说int array [32].

Rob*_*obᵩ 6

试试这个:

Fifo<int[32], Q_SIZE> f; 
Run Code Online (Sandbox Code Playgroud)

像这样:

#include <iostream>
template <class T, int N>
struct Fifo {
  T t;
};

int main () {
 const int Q_SIZE  = 32;
 Fifo<int[32],Q_SIZE> f;
 std::cout << sizeof f << "\n";
}
Run Code Online (Sandbox Code Playgroud)