std :: tuple参数中的类型顺序有什么影响吗? [英] Does type order in std::tuple arguments have any effects?
问题描述
假设我想在一个元组中存储三种类型
: int
, float
和 std :: vector< double>
Say I want to store three types in a tuple
: int
, float
and std::vector<double>
,
tuple<int, float, vector<int>> t;
与此之间有任何差异
tuple<vector<int>, int, float> t;
由于执行 tuple
类变量基数,我预期一个不同的布局为生产的类,但它以任何方式重要吗?在 tuple
中放置类型时(例如放最大的第一个等等),还有什么需要考虑的优化注意事项?
Due to the implementation of tuple
as a class of variadic bases, I'm expecting a different layout for the produced classes, but does it matter in any way ? Also are there any optimization considerations to take into account, when placing types in a tuple
(eg put the largest first etc) ?
推荐答案
标准对类型的实际布局没有任何限制。该顺序影响的唯一的事情是 std :: get< N>
, std :: tuple_element< N,T>
等等。
The standard doesn't place any restrictions on the actual layout of the types. The only things the order influences are the results of std::get<N>
, std::tuple_element<N, T>
and so on.
我知道libstdc ++和Visual C ++按照给定顺序的相反顺序排列类型; libc ++按给定顺序排列类型。这实质上意味着没有可移植的方式来选择总是产生最佳布局的订单。
I know that libstdc++ and Visual C++ lay out the types in reverse order of the order given; libc++ lays out the types in the order given. This essentially means that there is no portable way to pick an order that always produces the best layout.
但是其他订单也是可能的。允许实施元组,使用总是产生最小尺寸的布局,但是仍保留 std :: get< N>
的相同语义,等等。我不知道这样做的任何标准库实现。
Other orders are possible, though. An implementation is allowed to implement tuple with a layout that always produces minimal size but still preserves the same semantics for std::get<N>
and so on. I don't know of any standard library implementation that does this, though.
这篇关于std :: tuple参数中的类型顺序有什么影响吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!