本文介绍了如何将std :: array转换为std :: tuple?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个aux模块,以便在多态对象之间传递值,并且在某个时候已经有了

I am working on a aux module to pass values between polymorphic objects and at some point I have

std::array<void*, N>

并且需要转发

std::tuple<void*, void*, /* N times */>

我可以通过使用 index_sequence 或/和递归来找到一些解决方案,但是所有这些看起来都很笨重且难以阅读.
通过标准库,还有其他更直接的方法吗?

I can figure some solution with the use of index_sequence or/and recursions, but all of those look bulky and difficult to read.
Is there any more straightforward way to do this by the means of the standard library?

另外,以防万一-我是对的, std :: array 的布局是各个对象的密集集合,因此,等于 void ** 各自的长度,而元组的布局允许有间隙吗?

Also, just in case - am I right that the layout of std::array is a dense set of respective objects, thus equal to, lets say, void** of respective length, whereas the layout of tuple allows gaps?

推荐答案

如果您的实现支持,则可以使用 std :: tuple_cat .在某些实现中,它将尊重元组接口( std :: array 确实这样做)的任意数量的对象连接到单个平面元组中.连接一个类似元组的对象只会产生一个元组,其中包含所述源元组"成员的副本.

If your implementation supports it, you can use std::tuple_cat. Under some implementations it concatenates any number of objects that respect the tuple interface (which std::array does) into a single flat tuple. Concatenating a single tuple like object will just produce a tuple that holds copies of the members of said source "tuple".

std::array<void*, N> a;
auto b = std::tuple_cat(a);

std :: array 是一个聚合,它将在内部保存 void * [N] .因此,是的,元素之间将没有填充.未指定元组元素的布局.

A std::array is an aggregate the will hold a void*[N] internally. So yes, the elements will be without padding in between them. The layout of the tuple's elements is not specified to such an extent.

这篇关于如何将std :: array转换为std :: tuple?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:14