C++ 只有两个成员的 std::pair 和 std::tuple 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6687107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:31:55  来源:igfitidea点击:

Difference between std::pair and std::tuple with only two members?

c++visual-studio-2010tuplesstd-pairstdtuple

提问by Casey

Is there a difference between an std::pairand an std::tuplewith only two members? (Besides the obvious that std::pairrequires two and only two members and tuplemay have more or less...)

只有两个成员的anstd::pair和 an之间有区别std::tuple吗?(除了明显std::pair需要两个且只有两个成员并且tuple可能有更多或更少......)

采纳答案by Nicol Bolas

There are some differences:

有一些区别:

  1. std::tuplecan never be by standard-layout(at least, it's not requiredto be by the standard). Every std::pair<T, Y>is standard-layout if both Tand Yare standard-layout.

  2. It's a bit easier to get the contents of a pairthan a tuple. You have to use a function call in the tuplecase, while the paircase is just a member field.

  1. std::tuple永远不可能是标准布局(至少,标准不需要)。每一个std::pair<T, Y>是标准的布局如果两个TY是标准布局。

  2. 获取 a 的内容pair比获取 a 的内容要容易一些tuple。您必须在tuple案例中使用函数调用,而pair案例只是一个成员字段。

But that's about it.

但仅此而已。

回答by Arafangion

An std::tuple's name is longer (one extra character). More of those characters are typed with the right hand, so easier for most people to type.

Anstd::tuple的名字更长(一个额外的字符)。更多的字符是用右手输入的,因此对于大多数人来说更容易输入。

That said, std::paircan only have two values - not zero, one, three or more. TWO values. A tuple, however, has almost no semantic limitation on the number of values. An std::pair, therefore, is a more accurate, type safe type to use if you actually want to specify a pair of values.

也就是说,std::pair只能有两个值——不能是零、一、三或更多。两个值。然而,元组对值的数量几乎没有语义限制。std::pair因此,An是一种更准确的类型安全类型,可以在您确实想要指定一对值时使用。

回答by Stephen Lin

This is a very late answer but note that, because std::pairis defined with member variables, its size cannot be optimized using empty base class optimization(firstand secondmust occupy distinct addresses, even if one or both is an empty class). This exacerbated by whatever alignment requirements second_typehas, so in the worst case the resulting std::pairwill be basically twice the size it needs to be.

这是一个很晚的答案,但请注意,由于std::pair是用成员变量定义的,因此无法使用空基类优化来优化其大小(first并且second必须占用不同的地址,即使一个或两个都是空类)。任何对齐要求都会加剧这种情况second_type,因此在最坏的情况下,结果std::pair基本上是所需大小的两倍。

std::tupleonly allows access through helper functions, so it's possible for it to derive from either type if one or the other is empty, saving on the overhead. GCC's implementation, at very least, definitely does this...you can poke through the headers to verify this but there's also thisas evidence.

std::tuple只允许通过辅助函数访问,因此如果一个或另一个为空,它可以从任一类型派生,从而节省开销。GCC 的实现,至少,肯定是这样做的……你可以通过标题来验证这一点,但也有这个作为证据。

回答by bhardwajs

Note that with C++ 17, one can use the same interface to read data from both pair and tuple with two elements.

请注意,在 C++ 17 中,可以使用相同的接口从具有两个元素的对和元组读取数据。

auto [a, b] = FunctionToReturnPairOrTuple();

No need to use get<>:)

无需使用get<>:)

回答by tgoodhart

For what it's worth, I find the GDB output of std::tuple to be far more difficult to read. Obviously if you need more than 2 values then std::pair won't work, but I do consider this a point in favor of structs.

就其价值而言,我发现 std::tuple 的 GDB 输出更难以阅读。显然,如果您需要 2 个以上的值,那么 std::pair 将不起作用,但我确实认为这是支持结构的一点。