【发布时间】:2020-12-19 13:58:42
【问题描述】:
我们的目标是有一个函数,它可以接收任意数量的任意大小 N 的数组
#include <array>
using namespace std;
template<int N>
struct MyArray {};
// here I don't know how to deduce N and keep the variadic number of parameters
template<int N, typename... Ts>
MyArray<N> foo(const Ts&... ts)
{
// somehow use the variadic parameters
MyArray<N> a;
return a;
}
int main()
{
array<double, 3> a, b, c;
auto d = foo(a, b, c);
}
【问题讨论】:
-
@Scheff
N应该是作为参数给出的数组的大小,而不是数组的数量。显示的代码应该只处理std::array<double,3>。 -
抱歉,解释不好,目标是有一个函数,它可以接收任意数量的任意大小 N 的数组
(但这对所有人都可以),并且有N 在函数体内可用。例如,假设我想使用 N, MyArray 返回另一个用户定义的数组类型。我希望它更清楚!谢谢。
标签: c++ templates c++17 variadic-templates template-argument-deduction