Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板能否决定C+;中的参数数量+;?_C++_Templates - Fatal编程技术网

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个专门化,(每个版本的代码大多是重复的)或使用固定编号和特殊类型。。。