Templates g+中的模板函子错误+;
我有一些代码,我想使用映射中的映射值构建元素向量。下面的代码在VisualStudio中运行良好(据我所知,它似乎是合法的),但g++不同意Templates g+中的模板函子错误+;,templates,g++,overloading,functor,Templates,G++,Overloading,Functor,我有一些代码,我想使用映射中的映射值构建元素向量。下面的代码在VisualStudio中运行良好(据我所知,它似乎是合法的),但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
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>>));"
}
};
模板
typename对::第二个类型foo(const对和arg)
{
返回(参数秒);
}
甲级
{
私人:
typedef std::map\u t;
地图;
公众:
空条()
{
//尝试将映射类型从映射拉入向量
std::vectorvect(m_map.size());
std::transform(m_-map.begin()、m_-map.end()、vect.begin(),
&foo);//在我的机器上编译以下内容:
#include <map>
#include <vector>
#include <algorithm>
#include <string>
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()
{
std::vector<std::wstring>vect(m_map.size());
std::transform(m_map.begin(), m_map.end(), vect.begin(),
&foo<map_t::value_type>);
}
};
版本:
$ g++ --version
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5490)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
为我编译而没有问题(g++4.3.3-5ubuntu4).Even-Wall没有给出任何警告。适用于我、Debian、gcc 4.1、4.2和4.3。你有确切的包含文件和你使用的gcc版本吗?其他人说它也适用于他们。我不能给出原始来源,但我怀疑错误在某个地方,我无法轻易公开。无论如何,谢谢你的帮助我会帮你的!至少现在我知道这是可以做到的,我只是错过了一些东西。
$ g++ --version
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5490)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.