简体   繁体   English

为什么是std :: pair <A,B> 与std :: tuple不同 <A,B> ? (真的没办法吗?)

[英]Why is std::pair<A,B> not the same as std::tuple<A,B>? (Is there really no way?)

Why is std::pair<A,B> not the same as std::tuple<A,B> ? 为什么std::pair<A,B>std::tuple<A,B> It always felt strange to not be able to just substitute one with the other. 不能用另一个替换一个人总是感到奇怪。 They are somewhat convertible, but there are limitations. 它们有点可兑换,但也有局限性。

I know that std::pair<A,B> is required to have the two data members A first and B second , so it can't be just a type alias of std::tuple<A,B> . 我知道std::pair<A,B>需要有两个数据成员A firstB second ,所以它不能只是std::tuple<A,B>的类型别名。 But my intuition says that we could specialize std::tuple<A,B> , that is a tuple with exactly two elements, to equal the definition of what the standard requires a std::pair to be. 但是我的直觉说我们可以专门化std::tuple<A,B> ,这是一个具有两个元素的元组,等于标准需要std::pair的定义。 And then alias this to std::pair . 然后将其别名为std::pair

I guess this wouldn't be possible as it is too straight-forward to not to be already thought of, yet it wasn't done in g++'s libstdc++ for example (I didn't look at the source code of other libraries). 我想这是不可能的,因为它太过直接而不被人们想到了,但它没有在g ++的libstdc ++中完成(例如我没有查看其他库的源代码)。 What would the problem of this definition be? 这个定义的问题是什么? Is it "just" that it would break the standard library's binary compatibility? 它是否“会”破坏标准库的二进制兼容性?

You've gotta be careful about things like SFINAE and overloading. 你必须小心SFINAE和重载等事情。 For example, the code below is currently well-formed but you would make it illegal: 例如,下面的代码目前格式正确,但您会将其视为非法:

void f(std::pair<int, int>);
void f(std::tuple<int, int>);

Currently, I can disambiguate between pair and tuple through overload resolution, SFINAE, template specialization, etc. These tools would all become incapable of telling them apart if you make them the same thing. 目前,我可以通过重载解析,SFINAE,模板专业化等来消除对和元组之间的歧义。如果你把它们做成同样的事情,这些工具都将无法区分它们。 This would break existing code. 这会破坏现有代码。

There might have been an opportunity to introduce it as part of C++11, but there certainly isn't now. 可能有机会将它作为C ++ 11的一部分引入,但现在肯定没有。

This is purely historical. 这纯粹是历史性的。 std::pair exist since C++98 whereas tuple came after and was initially not part of the standard. std::pair从C ++ 98开始存在,而tuple来自并且最初不是标准的一部分。

Backward compatibility is the biggest burden for C++ evolution, preventing some nice things to be done easily ! 向后兼容性是C ++发展的最大负担,阻止了一些简单易用的事情!

I've not tried this and don't have the bandwidth right now to do so. 我没试过这个,现在没有带宽这么做。 You could try making a specialisation of std::tuple, derived from a sd::pair. 您可以尝试对std :: tuple进行专门化,派生自sd :: pair。 Someone please tell me this won't work or is particularly horrible idea. 有人请告诉我这不起作用或特别可怕的想法。 I suspect you'd run into trouble with accessors. 我怀疑你遇到了访问者的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 变换 std::pair <std::pair<std::pair<a, b> , C&gt;, D&gt; 到 std::tuple <a, b, c, d></a,></std::pair<std::pair<a,> - C++ transform std::pair<std::pair<std::pair<A, B>, C>, D> to std::tuple<A, B, C, D> 创建std :: pair时出现“ operator &lt;”错误<A,B> - 'operator<' error while creating an std::pair<A,B> 有没有一种很好的方法将std :: minmax(a,b)分配给std :: tie(a,b)? - Is there a nice way to assign std::minmax(a, b) to std::tie(a, b)? 我的哪种情况会std :: map <A,B> 比排序的std :: vector更快 <std::pair<A,B> &gt;? - I which situation will std::map<A,B> be faster than sorted std::vector<std::pair<A,B>>? 为什么`std :: pair`将`std :: tuple`作为ctor参数类型而不是`const std :: tuple&`? - Why does `std::pair` take `std::tuple` as ctor argument type rather than `const std::tuple&`? 使用std :: pair键和值在地图中添加b2vec2 - Adding a b2vec2 in a map with std::pair key and value 使用std :: pair或std :: tuple的移动语义 - Using move semantics with std::pair or std::tuple 将std :: tuple的类型转换为std :: pair - Convert type of std::tuple to std::pair 有没有办法使用相同的索引迭代 std::tuple 和 std::array ? - Is there a way to iterate over std::tuple and std::array using the same index? std::fabs(a * b) 和 std::fabs(a) * std::fabs(b) 之间的区别 - Difference between std::fabs(a * b) and std::fabs(a) * std::fabs(b)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM