Is there a convenient way to wrap std::pair as a new type?

后端 未结 8 1089
梦谈多话
梦谈多话 2021-02-02 17:02

Often times I find myself using std::pair to define logical groupings of two related quantities as function arguments/return values. Some examples: row/col, tag/value, etc.

8条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 17:47

    You can still reuse the pair functionality by forwarding to it:

    bool operator< ( const Position &a, const Position &b ) 
    {
        return
            std::make_pair( a.row, a.col ) < std::make_pair( b.row, b.col );
    }
    

    Although you still end up with doing this for every operatory you need...

提交回复
热议问题