Reputation: 45665
Why is std::pair<A,B>
not the same as 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>
. 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. And then alias this to 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). What would the problem of this definition be? Is it "just" that it would break the standard library's binary compatibility?
Upvotes: 14
Views: 843
Reputation: 146910
You've gotta be careful about things like SFINAE and overloading. 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. 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.
Upvotes: 3
Reputation: 2092
This is purely historical. std::pair
exist since C++98 whereas tuple came after and was initially not part of the standard.
Backward compatibility is the biggest burden for C++ evolution, preventing some nice things to be done easily !
Upvotes: 2
Reputation: 729
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. Someone please tell me this won't work or is particularly horrible idea. I suspect you'd run into trouble with accessors.
Upvotes: 0