如果编译器知道索引并且其他情况下行为正常,我可以对 std::array 进行编译时绑定检查吗?

问题描述 投票:0回答:1

作为一个简单的例子,考虑以下类:

template <int N>
class A {
private:
  std::array<double, N> m_vs = {};
public:
  int size() {return N;}
  void set_value(double d, int n=0) {m_vs[n] = d;}
  double get_value(int n=0) {return m_vs[n];}
}

假设我们有

a = A<10>()

如果我输入

a.set_value(1, 20)
,我希望编译器抛出错误,因为已知这将超出范围,但如果我输入
a.set_value(1, i)
,则不会抛出任何错误,因为
i
可能是有效的。

有可能出现这样的行为吗?

c++ arrays
1个回答
2
投票

您不会获得运行时值的编译时检查。你可以获得

  • 编译时检查您的
    constexpr
  • 运行时检查运行时值

示例:

template <std::size_t N>
class A {
private:
    std::array<double, N> m_vs = {};

public:
    constexpr std::size_t size() const { return N; }

    // use these with runtime `n`s:
    double get_value(std::size_t n) {
        if (n >= N) throw std::out_of_range("get_value: n >= N");
        return m_vs[n];
    }

    void set_value(double d, std::size_t n) {
        if (n >= N) throw std::out_of_range("set_value: n >= N");
        m_vs[n] = d;
    }

    // use these with `n`s known at compile time:
    template <std::size_t n = 0>
    double get_value() {
        static_assert(n < N, "n < N failed");
        return m_vs[n];
    }

    template <std::size_t n = 0>
    void set_value(double d) {
        static_assert(n < N, "n < N failed");
        m_vs[n] = d;
    }
};

用途:

int main() {
    A<10> a;

    a.set_value(3.141);      // n == 0, all's well (if N > 0, else compile time error)
    a.set_value<10>(3.141);  // compile time time error, 10 is out-of-bounds
    a.set_value(3.141, 10);  // runtime access out-of-bounds, throws exception
}
© www.soinside.com 2019 - 2024. All rights reserved.