Is there a difference between an std::pair
and an std::tuple
with only two members? (Besides the obvious that std::pair
requires two and only two members and tuple
may have less...)
std :: pair和std :: tuple之间只有两个成员有区别吗? (除了显而易见的是std :: pair需要两个且只有两个成员,而元组可能会少......)
4 个解决方案
#1
53
There are some differences:
有一些差异:
-
std::tuple
can never be by standard-layout (at least, it's not required to be by the standard). Everystd::pair<T, Y>
is standard-layout if bothT
andY
are standard-layout.std :: tuple永远不能通过标准布局(至少,它不需要通过标准)。如果T和Y都是标准布局,则每个std :: pair
都是标准布局。 ,y> -
It's a bit easier to get the contents of a
pair
than atuple
. You have to use a function call in thetuple
case, while thepair
case is just a member field.获取一对内容比使用元组更容易一些。您必须在元组大小写中使用函数调用,而对案例只是一个成员字段。
But that's about it.
但就是这样。
#2
20
This is a very late answer but note that, because std::pair
is defined with member variables, its size cannot be optimized using empty base class optimization (first
and second
must occupy distinct addresses, even if one or both is an empty class). This exacerbated by whatever alignment requirements second_type
has, so in the worst case the resulting std::pair
will be basically twice the size it needs to be.
这是一个非常晚的答案,但请注意,因为std :: pair是使用成员变量定义的,所以无法使用空基类优化来优化其大小(第一个和第二个必须占用不同的地址,即使其中一个或两个都是空类) 。这种情况因second_type具有的任何对齐要求而加剧,因此在最坏的情况下,生成的std ::对将基本上是它需要的两倍。
std::tuple
only 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 this as evidence.
std :: tuple只允许通过辅助函数进行访问,因此如果一个或另一个为空,它可以从任一类型派生,从而节省开销。 GCC的实现,至少,肯定是这样做的......你可以通过标题来验证这一点,但也有这个作为证据。
#3
17
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.
std :: tuple的名字更长(一个额外的字符)。用右手输入更多这些字符,大多数人都更容易打字。
That said, std::pair
can 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是一种更准确,类型安全的类型。
#4
3
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将不起作用,但我认为这是支持结构的一点。
#1
53
There are some differences:
有一些差异:
-
std::tuple
can never be by standard-layout (at least, it's not required to be by the standard). Everystd::pair<T, Y>
is standard-layout if bothT
andY
are standard-layout.std :: tuple永远不能通过标准布局(至少,它不需要通过标准)。如果T和Y都是标准布局,则每个std :: pair
都是标准布局。 ,y> -
It's a bit easier to get the contents of a
pair
than atuple
. You have to use a function call in thetuple
case, while thepair
case is just a member field.获取一对内容比使用元组更容易一些。您必须在元组大小写中使用函数调用,而对案例只是一个成员字段。
But that's about it.
但就是这样。
#2
20
This is a very late answer but note that, because std::pair
is defined with member variables, its size cannot be optimized using empty base class optimization (first
and second
must occupy distinct addresses, even if one or both is an empty class). This exacerbated by whatever alignment requirements second_type
has, so in the worst case the resulting std::pair
will be basically twice the size it needs to be.
这是一个非常晚的答案,但请注意,因为std :: pair是使用成员变量定义的,所以无法使用空基类优化来优化其大小(第一个和第二个必须占用不同的地址,即使其中一个或两个都是空类) 。这种情况因second_type具有的任何对齐要求而加剧,因此在最坏的情况下,生成的std ::对将基本上是它需要的两倍。
std::tuple
only 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 this as evidence.
std :: tuple只允许通过辅助函数进行访问,因此如果一个或另一个为空,它可以从任一类型派生,从而节省开销。 GCC的实现,至少,肯定是这样做的......你可以通过标题来验证这一点,但也有这个作为证据。
#3
17
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.
std :: tuple的名字更长(一个额外的字符)。用右手输入更多这些字符,大多数人都更容易打字。
That said, std::pair
can 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是一种更准确,类型安全的类型。
#4
3
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将不起作用,但我认为这是支持结构的一点。