简体   繁体   English

C ++非类型模板参数编译时间检查

[英]C++ non-type template parameter compile time check

I have a C++ class with two non-type template parameters: 我有一个带有两个非类型模板参数的C ++类:

A dimension of type size_t and a const reference to a vector of objects. 类型为size_t的维和对对象向量的const引用。

template <size_t DIMENSION, const std::vector<Tuple>& BORDERS>
class Rule {
public:

  Rule(const std::vector<Tuple>& ranges, const Action& action) :
      ranges_(ranges),
      action_(action) {};

private:

  std::vector<Tuple> ranges_;
  Action action_;
};

Is there a possibility to ensure at compile time that the following condition holds? 是否有可能在编译时确保满足以下条件?

DIMENSION == BORDERS.size()

Thank you in advance for any suggestions. 预先感谢您的任何建议。

A vector is a dynamic container, so its size depends on how you use it at run-time. vector是动态容器,因此其大小取决于您在运行时使用它的方式。 Unfortunately that means there is no compile-time check you can do to achieve what you want. 不幸的是,这意味着您无法进行编译时检查来实现所需的功能。

std::array may be more suitable in this case, as its size is fixed at compile time. 在这种情况下, std::array可能更适合,因为它的大小在编译时是固定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM