αλεχολυτ
αλεχολυτ

Reputation: 5039

Get std::pair member using only standard function in C++03

Is there a way, using only C++03 standard functions get a std::pair member, i.e. first or second?

In C++11 I could use std::get<0> or std::get<1> respectively in this case.

Upvotes: 2

Views: 195

Answers (2)

Vittorio Romeo
Vittorio Romeo

Reputation: 93264

There is no free function that allows you to retrieve std::pair::first and std::pair::second. It is, however, trivial to implement:

template <std::size_t TI, typename T>
struct get_helper;

template <typename T>
struct get_helper<0, T>
{
    typedef typename T::first_type return_type;

    return_type operator()(T& pair) const
    {
        return pair.first;
    }
};

template <typename T>
struct get_helper<1, T>
{
    typedef typename T::second_type return_type;

    return_type operator()(T& pair) const
    {
        return pair.second;
    }
};

template <std::size_t TI, typename T>
typename get_helper<TI, T>::return_type my_get(T& pair)
{
    return get_helper<TI, T>()(pair);
}

coliru example

Upvotes: 7

user541686
user541686

Reputation: 210402

No, there is not. If you want them, you'll have to make them yourself.

Upvotes: 2

Related Questions