将可变参数包缩短为 N 种
我想编写一个大小为 N (> 0) 和可变数量参数 (>= N) 的类。它应该有一个接受 N 个参数的构造函数和一个具有相同类型的成员 std::tuple
:
template <size_t N, typename... Args>
struct Example {
// A constructor taking N parameters of type Args[N], initializing the member tuple
// (e.g. param 1 has type Args[0], param 2 has type Args[1], ...,
// param N has type Args[N-1])
// A tuple with N elements, each corresponding to Args[N]
// (e.g. std::tuple<Args[0], ..., Args[N-1]>)
//For instance Example<3, int, float, int, bool> should result in
constexpr Example(int a, float b, int c): t(a, b, c) {}
std::tuple<int, float, int> t;
}
一般来说:这可能吗?如果没有,是否有可行的替代方案?为什么这有效/无效?我正在使用 C++20。
I would like to write a class that takes a size N
(> 0) and a variable number of arguments (>= N). It should have a constructor that takes N arguments and a member std::tuple
which has the same type:
template <size_t N, typename... Args>
struct Example {
// A constructor taking N parameters of type Args[N], initializing the member tuple
// (e.g. param 1 has type Args[0], param 2 has type Args[1], ...,
// param N has type Args[N-1])
// A tuple with N elements, each corresponding to Args[N]
// (e.g. std::tuple<Args[0], ..., Args[N-1]>)
//For instance Example<3, int, float, int, bool> should result in
constexpr Example(int a, float b, int c): t(a, b, c) {}
std::tuple<int, float, int> t;
}
In general: Is this possible? If not are there viable alternatives? Why does/ doesn't this work? I'm using C++20.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就我对这个问题的理解而言,它似乎只是在问如何从参数生成元组。其中,使用 Boost.Mp11, 是一个简短的单行代码(一如既往):
而不是
Example<3, int, float, int, bool>
是具有成员的某种类型tuple
带有一个构造函数,它实际上是tuple
。如果由于某种原因,您特别需要一个成员元组和指定的构造函数,我们可以很容易地做到:
To the extent I understand the question, it simply seems to be asking how to produce a
tuple
from arguments. Which, using Boost.Mp11, is a short one-liner (as always):Rather than
Example<3, int, float, int, bool>
being some type that has a membertuple<int, float, int>
with one constructor, it actually istuple<int, float int>
.If you, for some reason, specifically need exactly a member tuple and exactly the constructor specified, we can do easily enough:
这是使用 C++20 的实现:
我们在这里所做的是,首先使用
take
元函数删除额外的模板参数。然而,由于类型包在 C++ 中不是一流的,因此我们只能采用所需类型的typelist
。为了从给定的typelist
创建元组 + 构造函数,我们有一个名为make_ctor
的辅助类型。通过继承它的构造函数,我们就得到了想要的API。对于生产,请参阅巴里的答案并使用现有的库进行元编程部分。
Here is an implementation using C++20:
What we do here is, first we drop the extra template arguments using the
take
meta-function. However, since type packs are not first class in C++, we only get to take atypelist
of our desired types. To create a tuple + a constructor from a giventypelist
, we have a helper type calledmake_ctor
. By inheriting its constructor, we get the desired API.For production, see Barry's answer and use an existing library for the metaprogramming part.
使用
std::tuple_element
和辅助基类,在我看来,你可以编写如下内容(C++14 就足够了)Using
std::tuple_element
and an helper base class, seems to me that you can write something as follows (C++14 is enough)您可以使用
make_index_sequence
来帮助从Args...
中提取第一个N
。例子:
You could
make_index_sequence<N>
to help with extracting the firstN
fromArgs...
.Example:
有所谓的模板推导指南。下面的代码中用注释“//模板推导指南”进行了标记。
本指南展示了如何从构造函数的参数推断结构的模板类型。
现在您可以使用任何类型调用构造函数(就像我在 main() 中所做的那样)。
在线尝试!
输出:
There are so-called Template Deduction Guides. It is marked in code below with comment "// Template Deduction Guide".
This guide shows how to deduce template types of a structure from arguments of constructor.
Now you can call constructor (as I did in main()) with any types.
Try it online!
Output: