Search code examples
c++aliaskey-valuestd-pair

How to alias pair member-data in C++?


How to alias pair member-data in C++ ?

I have a container of std::pair<int,int> to store master-slave pairs.

For better readability I want to use something like:

using Master = std::pair<int,int>::first;

std::pair<int,int> myPair;
auto myMaster = myPair.Master;

What would be the correct syntax here?

Thank you, in advance.


Solution

  • An alternative would be to use the tuple-like get access to std::pair with a self-defined constant:

    const std::size_t Master = 0;
    
    std::pair<int,int> myPair;
    auto myMaster = std::get< Master >( myPair );
    

    Somewhat cleaner and more readable than pointered access IMHO.