以下代码将无法编译。编译器抱怨*没有匹配函数调用 for_each*。为什么会这样?
#include <map>
#include <algorithm>
struct Element
{
void flip() {}
};
void flip_all(std::map<Element*, Element*> input)
{
struct FlipFunctor
{
void operator() (std::pair<Element* const, Element*>& item)
{
item.second->flip();
}
};
std::for_each(input.begin(), input.end(), FlipFunctor());
}
当我struct FlipFunctor
在 function 之前移动时flip_all
,代码会编译。
完整的错误信息:
没有匹配函数调用 'for_each(std::_Rb_tree_iterator<std::pair<Element* const, Element*> >, std::_Rb_tree_iterator<std::pair<Element* const, Element*> >, flip_all(std ::map<Element*, Element*, std::less<Element*>, std::allocator<std::pair<Element* const, Element*> > >)::FlipFunctor)'</p>