可变参数模板根据未知数量的参数返回 N 元组
我想要一个可变参数函数模板,它接受某种类型 T 的指针,填充这些指针,并为每个指针生成一个对象作为结果,最终结果是所有这些生成的对象的元组。
给定一个库中的函数(使用 C api):
struct opaque {};
bool fill_pointer(opaque **);
并且:
struct MyType {};
MyType gen_object(opaque *);
我想要一个可变参数模板函数,看起来像这样(有点):(
std::tuple<bool, MyType...> fill_and_gen_objects(opaque **...);
其中 bool
结果是 false
当且仅当 fill_pointer
返回值之一为 false
时)。
这就是我想要实现的目标:
opaque *oa, *ob, *oc;
auto [failed, ta, tb, tc] = fill_and_gen_objects(oa, ob, oc);
谢谢
I would like to have a variadic function template that takes pointers of a certain type T, fills those pointers, and for each of them generate an object as a result, the final result being a tuple of all these generated objects.
Given a function from a library (that uses a C api):
struct opaque {};
bool fill_pointer(opaque **);
And:
struct MyType {};
MyType gen_object(opaque *);
I would like to have a variadic template function that would look like this (sort of):
std::tuple<bool, MyType...> fill_and_gen_objects(opaque **...);
(where the bool
result is false
if and only one of fill_pointer
return value is false
).
This is what I would like to achieve:
opaque *oa, *ob, *oc;
auto [failed, ta, tb, tc] = fill_and_gen_objects(oa, ob, oc);
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
continue
That's heavy pseudocode, I'll answer with heavy pseudocode:
Ok, actually it even compiles, see here
EDIT: downgraded to C++14 ... that's what you've tagged.
Same for C++17 using CTAD
Same for C++20 using abbreviated function template syntax
C++20 with indices by using integer sequence (untested):
Furthermore, here is the C++27 solution: