使std:数组大小取决于类模板参数

Make std:array size depending on class template parameter

本文关键字:参数 取决于 std 数组      更新时间:2023-10-16

让我们考虑以下非常简单的例子

#include <array>
template<typename T, F>
class GenericColor {
protected:
   std::array<T, F> components;
}
class RGB : public GenericColor<int, 3> { // three components, red, green...
}
class CMYK : public GenericColor<int, 4> { // four components, cyan, magenta....
}

我的问题是如何使第二个参数指向std::数组大小,只是为了使这个例子工作

std::array的声明为*

template<class T, std::size_t N> struct array

告诉你如何写代码:

template<typename T, std::size_t F>
class GenericColor {
protected:
   std::array<T, F> components;
}

*这里有一些空洞的细节,不需要完全是这样的,但是blah blah blah不相关