在C ++中对数组进行静态绑定检查 [英] Static bound checking for array in C++

查看:51
本文介绍了在C ++中对数组进行静态绑定检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关我正在学习的编程语言课程中的问题的指导。

I'm in need for some guidance regarding a question from a programming languages course I'm taking.

我们需要想出一种方法来实现C ++中的数组类,以便对访问其元素进行静态检查是否溢出。
我们不要使用C ++ 11(静态断言)或任何其他黑盒解决方案-这是一个理论问题,不是我出于编码目的所需要的。

We need to come up with a way to implement an array class in C++, so that accessing it's element is statically checked for overflow. We are not to use C++11 (static assertion) nor any other black-box solution - this is a theoretical question, not something I need for coding purposes.

我们在演讲幻灯片中确实得到了强烈的暗示:

we did get a strong hint in the lecture slides :


当索引的类型为整数时,不可能检测到数组索引的溢出–如果索引的类型对应于数组大小(必须是数组类型的一部分),则不是这样。

" it is impossible to detect overflow of array indices when indices are of type integer – not if the type of the indices corresponds to the array size (which must be part of the type of the array). "

我考虑过使用固定长度(数组大小)的索引字符串,但是除了考虑该选项之外,我的确没有得到太多的好处:(。

I thought about using fixed-length (array size) string for indices, but other than thinking about the option I really did not get much :(.

对此感到困惑,可能是因为我不清楚-所以我将再次强调:

Clarification: OK, this has got some confused replies to it, probably because I was unclear - So I will re-emphasize:


  1. 静态地的意思是在编译时。 编译器应该警告用户溢出(警告/错误..),而不是运行时!

  2. 阅读提供给我们的提示具体来说,该程序可能不会检查界限!具体地说,不要使用或模拟c ++中的数组的at()方法。

  1. "Statically" means "at compile time". The compiler should alert the user of the overflow (Warning/Error..). Not runtime!
  2. Read the "hint" given to us - it's to be used. specifically, the program may NOT check for bounds! specifically the at() method of arrays in c++ is not to be used or emulated.

鉴于所有这些,我在想他们想要的是某种
int->(索引类型)
的转换,如果数组溢出,它会以某种方式失败或为这些索引计算错误的值。

In light of all this what I am thinking they want is some sort of transformation int->(Indices type) that somehow fails or computes wrong values for these indices in case the array is overflowed.

希望现在更加清晰。谢谢

Hope that is clearer now. Thank you's

推荐答案

也许他打算让您根据值是该类型一部分的类型对数组进行索引,例如为 std :: integral_constant< int,value> 。使用此功能,可以在编译时检查大小。但是,如果没有 static_assert ,就很难想到简单的方法来断言一个常数小于另一个常数。

Maybe he intends for you to index the array based on a type where the value is part of the type, such as std::integral_constant<int, value>. Using this, the size can be checked at compile time. However, without static_assert, it's hard to think of simple ways to assert that one constant is smaller than another.

在这里,我使用比较技巧来比较索引是否小于大小,然后将其转换为整数(如果超出范围则为0,否则为1),将其乘以2并减去1,得到一个(-1表示超出范围,或1表示有效),然后制作该大小的数组。如果索引超出范围,则会导致编译错误;如果索引有效,则会导致优化错误。

Here, I use the trick of comparing if the index is less than the size, converting that to an integer (0 if it's out of bounds, or 1 otherwise), multiplying that by two and subtracting one, giving either (-1 for out of bounds, or 1 for valid), and then making an array of that size. This causes a compilation error if the index is out of bounds, and is optimized out if the index is valid.

#include <type_traits>

template<class T, int count>
struct array {
    array() : data() {}

    template<int N>
    T& operator[](std::integral_constant<int,N> index) {
        //compiler error if index is too small
        char index_is_too_large[(N<count)*2-1]={}; 
        index_is_too_large[0] = index_is_too_large[0]; 
        //second line makes the compiler ignore warnings

        return data[N];
    }
private:
    T data[count];
};

#include <iostream>
int main() {
    array<float, 3> thingy;

    std::integral_constant<int, 2> index2;
    std::cout << thingy[index2] << '\n';

    std::integral_constant<int, 3> index3;
    std::cout << thingy[index3] << '\n'; //COMPILER ERROR ON THIS LINE
}

您,我认为您很可能会误解您的教授。

This seems quite advanced for the restrictions placed on you, I think it's more likely you misunderstood your professor somewhere along the line.

这篇关于在C ++中对数组进行静态绑定检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆