-2
我有下面的递归函数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 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
它产生,垃圾结果调用。我认为这是一个无符号溢出的问题,但我不太清楚它是如何发生的。我检查了gcc
和clang
。
请向我们展示一个完整的独立程序,其输出显示问题以及实际输出。 –
为什么人们不能再遵循简单的指示?总是必须要求一个MCVE。叹。 –