我从github下载了一个C ++库来支持多维数组,当我使用GCC8.2.0构建它时,编译器遇到一条消息,说重载函数不明确。考虑以下代码:
// basic function, set the length i for the dimension D
template<int Rank,int D>
void set_array(std::array<int,Rank>& A,int i)
{
A[D] = i;
}
// function with parameter pack, set the length for all dimensions recursively
template<int Rank,int D,typename... indices>
void set_array(std::array<int,Rank>& A,int i,indices... idx)
{
A[D] = i;
set_array<Rank,D+1>(A,idx...);
}
// class calls set_array in allocation
template<typename T,int Rank>
class MultiArray
{
private:
std::array<int,Rank> _dim = {{0}};
template<typename... indices>
void allocate(indices... idx) {
static_assert(sizeof...(idx) == Rank,
"NUMBER OF INDICES PASSED TO ALLOCATE DOES NOT MATCH RANK OF ARRAY");
set_array<Rank,0>(_dim,idx...);
}
};
// code to create object of above MultiArray class.
MultiArray<int, 1> a;
a.allocate(10)
编译器错误消息为:重载“ set_array <1,0>(std :: array&,int&)的调用不明确,
我的理解是调用set_array <1,0>(_ dim,10)时,编译器无法知道应该使用哪一个,因为参数包可以为空。我尝试了一些修复方法,但失败了。有什么解决方案可以帮助我吗?预先谢谢!
解决这个问题的一种直接方法是,通过展开一次,至少需要一个参数来进行第二次重载
template<int Rank,int D,typename index0, typename... indices>