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

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

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 more or less...)

推荐答案

有一些区别:

  1. std::tuple 标准并不要求永远是 standard-layout.如果 TY 都是标准布局,则每个 std::pair 都是标准布局.

  1. std::tuple is not required by the standard to ever be standard-layout. Every std::pair<T, Y> is standard-layout if both T and Y are standard-layout.

获取 pair 的内容比获取 tuple 的内容要容易一些.您必须在 tuple 情况下使用函数调用,而 pair 情况只是一个成员字段.

It's a bit easier to get the contents of a pair than a tuple. You have to use a function call in the tuple case, while the pair case is just a member field.

但仅此而已.

相关文章