More expressive pairs

Started by
7 comments, last by Krohm 12 years, 5 months ago
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.

Previously "Krohm"

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.
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);
}
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.
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.

Previously "Krohm"


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.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

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.

Previously "Krohm"

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.
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.

Previously "Krohm"

This topic is closed to new replies.

Advertisement