仅使用C++ 03中的标准函数获取std :: pair成员

αλε*_*λυτ 2 c++ c++03

有没有办法,只使用C++ 03标准函数获得std::pair成员,即firstsecond

在C++ 11中,我可以分别使用std::get<0>或者std::get<1>在这种情况下.

Vit*_*meo 7

没有允许您检索std::pair::first和使用的免费功能std::pair::second.然而,实施起来是微不足道的:

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);
}
Run Code Online (Sandbox Code Playgroud)

coliru的例子