【发布时间】:2020-06-02 13:11:25
【问题描述】:
我从 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(std::array&, int&) 不明确,
我的理解是在调用set_array(_dim, 10)时,编译器不知道应该使用哪一个,因为参数包可以为空。我尝试了一些方法来修复但失败了。有什么解决方案可以帮助我吗?提前谢谢!
【问题讨论】:
-
代码的哪一部分来自lib,哪一部分是你的?
标签: c++ templates ambiguous variadic