有没有办法,只使用 C++03 标准函数得到 std::pair
成员,即 first
或 second
?
在 C++11 中我可以使用 std::get<0>
或 std::get<1>
分别在这种情况下。
最佳答案
没有允许您检索 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);
}
关于c++ - 在 C++03 中仅使用标准函数获取 std::pair 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42023548/