C++ 模板能否决定C+;中的参数数量+;?
考虑这样一个类C++ 模板能否决定C+;中的参数数量+;?,c++,templates,C++,Templates,考虑这样一个类 template<int n, class T> class dimArray { T* arr; int len[n]; public: dimArray(int len₀, int len₁, int len₂, ..., int lenₕ₋₁, T initValue); T& at(int x₀, int x₁, int x₂, ..., int xₕ₋₁); void foo(int x₀, int x₁, i
template<int n, class T>
class dimArray {
T* arr;
int len[n];
public:
dimArray(int len₀, int len₁, int len₂, ..., int lenₕ₋₁, T initValue);
T& at(int x₀, int x₁, int x₂, ..., int xₕ₋₁);
void foo(int x₀, int x₁, int x₂, ..., int xₕ₋₁, .../*va_args*/);
}
模板
类dimArray{
T*arr;
内伦[n];
公众:
dimArray(内部透镜)₀, 内透镜₁, 内透镜₂, ..., 内透镜ₕ₋₁, T初始值);
T&at(int x)₀, 整数x₁, 整数x₂, ..., 整数xₕ₋₁);
void foo(int x₀, 整数x₁, 整数x₂, ..., 整数xₕ₋₁, .../*va_args*/);
}
被使用
dimArray<2, double> a(3,4, 1.0);
a.at(1,2) = 4.3;
std::cout << a.at(2,3);
a.foo(1,2, 7.3,4.2,0);
DIMA阵列(3,4,1.0);
a、 at(1,2)=4.3;
std::cout您可以使用可变模板和std::index\u序列
实用程序:
template <std::size_t I, typename T>
using always_t = T;
template<typename Seq, class T>
class dimArrayImpl;
template<std::size_t ... Is, class T>
class dimArrayImpl<std::index_sequence<Is...>, T>
{
T* arr;
int len[sizeof...(Is)];
public:
dimArrayImpl(always_t<Is, int>... lens, T initValue);
T& at(always_t<Is, int>... lens);
void foo(always_t<Is, int>... lens, .../*va_args*/);
};
template<int n, class T>
using dimArray = dimArrayImpl<std::make_indexsequence<n>, T>;
模板
始终使用\u t=t;
模板
类dimArrayImpl;
模板
类dimArrayImpl
{
T*arr;
国际长度[尺寸…(Is)];
公众:
dimArrayImpl(始终为镜头,t初始值);
T&at(始终为镜头);
void foo(总是镜头,/*va_args*/);
};
模板
使用dimArray=dimArrayImpl;
请发布一些示例代码,说明您希望如何使用这些函数。@RSahu done[])这可能是吗?这是基于C++14的吗?std::index_sequence
确实是C++14,但可以在C++11中实现。可变模板来自C++11。要在C++98中“模拟”它,您必须编写多达N个专门化,(每个版本的代码大多是重复的)或使用固定编号和特殊类型。。。