【问题标题】:Make std:array size depending on class template parameter根据类模板参数制作 std:array 大小
【发布时间】:2014-05-27 23:10:29
【问题描述】:

让我们考虑以下非常简化的示例

#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::array 大小,只是为了使这个示例正常工作。

【问题讨论】:

    标签: c++ templates standard-library


    【解决方案1】:

    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;
    }
    

    *这里有一些空洞的细节,不需要完全如此,但废话废话不相关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 2011-09-23
      相关资源
      最近更新 更多