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

标签 c++ c++03

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

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

最佳答案

没有允许您检索 std::pair::firststd::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);
}

coliru example

关于c++ - 在 C++03 中仅使用标准函数获取 std::pair 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42023548/

相关文章:

c++ - 如何将 boost::bind(&myClass::fun, this, _1, _2, _3) 转换为 typedef void (*fun)(arg1, arg2, arg3)?

C++ std:.auto_ptr 或 std::unique_ptr(支持多个编译器,甚至是旧的 C++03 编译器)?

c++ - 查找指向 NULL 的指针

c++ - 模板函数参数继承

c++ - 将变量形式 make 移植到 Cmake

C++ 单例实现Meyer's vs call_once

c++ - 如何从设备相关的 HBITMAP 构造 GDI+ 位图对象

c++ - 为什么非阻塞套接字在 connect() 或 accept() 之前是可写的?

C++ 替代模板化数据成员

c++ - 在 C++03 中返回类似 `std::auto_ptr` 的集合的最佳方法是什么?