C++ 这段代码如何产生无符号整数溢出

C++ 这段代码如何产生无符号整数溢出,c++,c++11,C++,C++11,我有以下递归constexpr函数 template<size_t N, size_t O,size_t All> constexpr int get_indices(const std::array<size_t,N> &products, const std::array<size_t,N>& idx, const std::array&

我有以下递归
constexpr
函数

template<size_t N, size_t O,size_t All>
constexpr int get_indices(const std::array<size_t,N> &products,
                          const std::array<size_t,N>& idx,
                          const std::array<std::array<int,O>,All> &as_all,
                          int i,
                          int it) {

    return it==0 ? as_all[i][idx[static_cast<int>(N)-1]] :
        products[it]*as_all[i][idx[it]] +
        get_indices(products,idx,as_all,i,it-1);
}
模板
constexpr int get_索引(const std::array&products、,
常量标准::数组和idx,
const std::数组和as_all,
int i,
int(it){
返回它==0?as_all[i][idx[static_cast(N)-1]:
产品[it]*as_all[i][idx[it]]+
获取指数(产品、idx、全部、i、it-1);
}
打电话时

 constexpr std::array<size_t,2> products = {2,0};
 constexpr std::array<size_t,2> idx = {0,1};
 constexpr std::array<std::array<int,3>,8> as_all = {{{0, 0, 0}, 
      {0, 0, 1}, 
      {0, 1, 0}, 
      {0, 1, 1}, 
      {1, 0, 0}, 
      {1, 0, 1}, 
      {1, 1, 0}, 
      {1, 1, 1}}};

get_indices(products,idx,as_all,4,2); // call it
constexpr std::array products={2,0};
constexpr std::array idx={0,1};
constexpr std::数组as_all={{{0,0,0},
{0, 0, 1}, 
{0, 1, 0}, 
{0, 1, 1}, 
{1, 0, 0}, 
{1, 0, 1}, 
{1, 1, 0}, 
{1, 1, 1}}};
获取_索引(乘积,idx,as_all,4,2);//叫它

它产生垃圾,产生垃圾。我认为这是一个无符号溢出的问题,但我不太确定它是如何发生的。我用
gcc
clang
进行了检查

您有一个越界访问权限,clang和gcc甚至会告诉您:


问题是您试图访问
idx[2]
,而它的大小是2,因此您只能访问
idx[0]
idx[1]

请向我们展示一个完整的自包含程序,其输出与实际输出一起显示问题。为什么人们不能再遵循简单的说明?总是需要一个MCVE。唉。
main.cpp:28:15: error: constexpr variable 'x' must be initialized by a constant expression

constexpr int x = get_indices(products,idx,as_all,4,2);
              ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

main.cpp:11:9: note: read of dereferenced one-past-the-end pointer is not allowed in a constant expression
        products[it]*as_all[i][idx[it]] +
        ^