Jump to content
  • Advertisement
Sign in to follow this  
Krohm

More expressive pairs

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!