gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:43:45 25 4
gpt4 key购买 nike

有没有办法,只使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com