我有一些代码,我想使用地图中的映射值构建元素向量。下面的代码在 Visual Studio 中运行良好(据我所知似乎是合法的),但 g++ 不同意。
template<class PAIR>
typename PAIR::second_type foo(const PAIR& arg)
{
return (arg.second);
}
class A
{
private:
typedef std::map<int, std::wstring> map_t;
map_t m_map;
public:
void bar()
{
// Attempt to pulled the mapped type from the map into the vector
std::vector<std::wstring>vect(m_map.size());
std::transform(m_map.begin(), m_map.end(), vect.begin(),
&foo<map_t::value_type>); // <-- error here, see below, also
// other attempts that all failed:
// - std::transform(..., boost::bind(foo<map_t::value_type>, _1));
// - std::transform(..., boost::bind(&map_t::value_type::second, _1));
// - also tried casting foo to a specific function type
// - also tried "template<class T> T itself(T arg) { return T; }" applied to all the above functor candidates, a la "std::transform(..., itself(<<functor>>));"
}
};
不幸的是,我现在没有确切的错误文本(关于无法弄清楚要使用哪个重载函数)或 g++ 的特定版本(最新版本与 Ubuntu 一起分发),但我会当我得到它时更新这篇文章。
同时,谁能解释为什么 g++ 不能解析所提供的函子的类型?