我正在尝试创建一个函数,该函数返回数组数组的零填充数组...在我定义的现代 C++ 中优雅地定义多维数组之后:
template<typename U, std::size_t N, std::size_t... M>
struct myTensor{
using type = std::array<typename myTensor<U, M...>::type, N>;
};
template<typename U, std::size_t N>
struct myTensor<U,N>{
using type = std::array<U, N>;
};
template<typename U, std::size_t... N>
using myTensor_t = typename myTensor<U, N...>::type;
然后我定义以下模板函数来填充零:
template<typename U, std::size_t N, std::size_t... M>
myTensor_t<U, N, M...> Zero_Tensor(){
myTensor_t<U, N, M...> res;
for(int i=0; i<N; i++)
res[i] = Zero_Tensor<U, M...>();
return res;
};
template<typename U, std::size_t N>
myTensor_t<U, N> Zero_Tensor(){
myTensor_t<U, N> res;
for(int i=0; i<N; i++)
res[i] = U(0);
return res;
};
例如,当我这样做时
class myclass{
myTensor_t<int,3,3,5> x;
};
它编译得很好。如果我尝试这样做:
class myclass{
myTensor_t<int,3,3,5> x=Zero_Tensor<int,3,3,5>();
};
我在编译时收到以下错误:
src/mytensor.hpp(107): error: no instance of overloaded function "Zero_Tensor" matches the argument list
res[i] = Zero_Tensor<U, M...>();
^
src/mytensor.hpp(112): note: this candidate was rejected because function is not visible
myTensor_t<U, N> Zero_Tensor(){
^
src/mytensor.hpp(104): note: this candidate was rejected because at least one template argument could not be deduced
myTensor_t<U, N, M...> Zero_Tensor(){
^
detected during:
instantiation of "myTensor_t<U, N, M...> Zero_Tensor<U,N,M...>() [with U=int, N=5UL, M=<>]" at line 107
instantiation of "myTensor_t<U, N, M...> Zero_Tensor<U,N,M...>() [with U=int, N=3UL, M=<5UL>]" at line 107
instantiation of "myTensor_t<U, N, M...> Zero_Tensor<U,N,M...>() [with U=int, N=3UL, M=<3UL, 5UL>]" at line 36 of "src/myclass.hpp"
我真的不明白this candidate was rejected because function is not visible
在告诉我什么。我想我不明白为什么它不可见?任何帮助表示赞赏。