# More expressive pairs

This topic is 2303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a way to have a pair object where I can also set contained value's names?
[font="'Courier New"]std::pair[/font]'s [font="'Courier New"]first [/font]and [font="'Courier New"]second [/font]are no much of a big deal. They just screwed me again.
Of course when it comes to parameter passing, only the types are relevant, ideally, it should be co-variant with standard [font="'Courier New"]std::pair [/font]as long as the base types are the same.
I have the feel I'm missing something here.

##### Share on other sites

Is there a way to have a pair object where I can also set contained value's names?

Not in C++. Not in pretty much any language if I understand you correctly.

##### Share on other sites
Don't know how helpful this would be..

 template< class X, class Y > class my_pair { public: X aye; Y bee; operator std::pair< X, Y > () { return std::make_pair(aye, bee); } my_pair &operator = (std::pair< X, Y > const &rhs_) { aye = rhs_.first; bee = rhs_.second; return *this; } }; int main() { my_pair< int, int > pair; std::pair< int, int > a = pair; pair = std::pair< int, int >(5, 6); } 

##### Share on other sites
I usually just make a struct. Rarely does the struct need all the features pair gives you. It would be nice if C++ gave you more options for code-generation, so I could avoid writing out dozens of member-wise equality operators and such, but that's life in C++ land.

##### Share on other sites
Well, I also usually just made my own struts.
Since everyone keeps telling me I should use more STL, i gave std::pair a go... but it didn't really survive the self-documenting requirement.
I will phase it out.

##### Share on other sites

Well, I also usually just made my own struts.
Since everyone keeps telling me I should use more STL, i gave std::pair a go... but it didn't really survive the self-documenting requirement.
I will phase it out.

Don't be confused with STL pair and a struct with two members.

Only use pair in common accepted convention.
One common convention is that a pair is a kind of key-value pair, which is used in associated data structure.
Another common convention is such as a pair of <string, WhatEverObject> is a kind of name-value pair.

For other less common situation, use a struct instead of pair. Use struct { int age; int language }; instead of pair<int, int>, the later is too fuzzy.

##### Share on other sites
Only use pair in common accepted convention.
One common convention is that a pair is a kind of key-value pair, which is used in associated data structure.
Another common convention is such as a pair of <string, WhatEverObject> is a kind of name-value pair.
Basically this is zero value for me.

##### Share on other sites
Off the top of my head:
 #define MY_PAIR(name, FirstType, first_name, SecondType, second_name) \ struct name { \ typedef std::pair<FirstType, SecondType> PairT; \ \ first_type first_name; \ second_type second_name; \ \ name() {} \ name(FirstType first, SecondType second) : first_name(first), second_name(second) {} \ name(const PairT & pair) : first_name(pair.first), second_name(pair.second) {} \ \ name & operator=(const PairT & rhs) { first_name = first; second_name = second; return * this; } \ \ operator PairT &() { return *reinterpret_cast< PairT *>(this); } \ operator const PairT &() const { return *reinterpret_cast<const PairT *>(this); } \ }; 
Though you'd need more code if you want rvalue reference support as well.

##### Share on other sites
Oh well, that would surely do... I'll have to think if I really want to have macros like that around. But at least that's tackling the problem. Thank you.