Reputation: 11
I know that "std::pair" struct does not inherit from "std::tuple" class. However, I wanted to make "pair" class which inherited from "std::tuple" class.
Is this correct? Are there any other ways?
#include <tuple>
template <typename First, typename Second>
class pair : public std::tuple<First, Second> {
public:
First &first = std::get<0>(*this);
Second &second = std::get<1>(*this);
using std::tuple<First, Second>::tuple;
};
Upvotes: 0
Views: 212
Reputation: 238311
Is this correct?
Depends on what you intend to do with the class. If the intention is for it to work like std::pair
, then it's not correct. Consider following example:
std::pair<int, int> std_p1{}, std_p2{};
std_p1 = std_p2; // works
pair<int, int> rin_p1{}, rin_p2{};
rin_p1 = rin_p2; // doesn't work
Are there any other ways?
Certainly. A good way is to not define a custom class inheriting from tuple, but to use std::pair
instead. That said, I find it difficult to find any use cases for pair types in the first place as in most cases named classes tend to be superior.
Upvotes: 2