is there accessible proceed hang std::pair new type?


often times i myself controlling std::pair interpretation judicious groupings twin associated quantities duty arguments/return values. examples: row/col, tag/value, etc.



often times i should unequivocally rolling possess category instead only controlling std::pair. it's graceful easy things start defilement down - formula becomes unwashed make_pair, first, second, the unequivocally tough remember what - an std::pair<int, int> conveys reduction definition form position.



what have found best ways hang functionality std::pair form conveys genuine meaning?



here things i have considered:



typedef std::pair<int, int> position;


this during slightest gives form revealing name flitting around, nonetheless form isn't enforced, the still unequivocally only pair, many same problems still exist. however unequivocally rudimentary write.



struct position : open std::pair<int, int>
{
typedef std::pair<int, int> base;
position() : base() {}
position(const position &x) : base(x) {}
position(int a, int b) : base(a, b) {}

int &row() { relapse first; }
const int &row() const { relapse first; }

int &col() { relapse second; }
const int &col() const { relapse second; }
};


this better, given opening variables around graceful detailed name. problem here still opening initial second, the easy condensation leak. also, accessing rudimentary variables around functions creates syntax annoying.



the apparent unbroken step estate private:



struct position : private std::pair<int, int>
{
typedef std::pair<int, int> base;
position() {}
position(const position &x) : base(x) {}
position(int a, int b) : base(a, b) {}

int &row() { relapse first; }
const int &row() const { relapse first; }

int &col() { relapse second; }
const int &col() const { relapse second; }

bool operator<(const position &x) const { relapse base(*this) < base(x); }
// forwarding operators needed...
};


so during slightest have gotten absolved opening initial second, nonetheless new problem pops up. wish store form an std::set, don't have opening operator< profusion given don't have opening initial second. means have interpretation forwarding duty any user profusion want. me wholly ==, !=, <, nonetheless there others i'd want. approbation i know i substantially shouldn't profusion operator< only hang an associative container, nonetheless creates all damn simple... defining operators any new form pain, still have opening around functions. repair that:



struct position
{
position() {}
position(const position &x) : row(x.row), col(x.col) {}
position(int row, int col) : row(row), col(col) {}

int row, col;
};
bool operator<(const position &a, const position &b)
{
relapse a.row < b.row || (!(b.row < a.row) && a.col < b.col);
}
// some-more overloads needed


so have rudimentary non-static access, nonetheless defining overloaded operators even some-more pain, since instead only forwarding pair's implementation, indeed have re-implement any time...



are there any solutions i have abandoned easy but drawbacks? there aren't tend prefer?



Comments

Popular posts from this blog

list macos calm editors formula editors

how i practical urls indicate .aspx pages asp.net deployed an iis? (preferably but iis)

jaxb - xjc - reworking generated typesafe enum category members