简体   繁体   English

为什么函数内定义的结构不能用作std :: for_each的函子?

[英]Why can't a struct defined inside a function be used as functor to std::for_each?

The following code won't compile. 以下代码将无法编译。 The compiler complains about *no matching function for call to for_each*. 编译器抱怨*没有匹配函数来调用for_each *。 Why is this so? 为什么会这样?

#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());
}

When I move struct FlipFunctor before function flip_all , the code compiles. 当我移动struct FlipFunctor之前功能flip_all ,代码编译。

Full error message: 完整的错误消息:

no matching function for call to '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)' 没有用于调用'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)'

std::for_each is a function template; std::for_each是一个函数模板; one of the template parameters is the type of the function argument. 其中一个模板参数是函数参数的类型。

You cannot use a local type as a template argument. 您不能将本地类型用作模板参数。 It's just a restriction currently in the language. 这只是目前语言中的限制。 In the forthcoming revision of C++, C++0x, this restriction is removed, so you can use local types as template arguments. 在即将推出的C ++,C ++ 0x版本中,此限制已被删除,因此您可以使用本地类型作为模板参数。

Visual C++ 2010 already supports the use of local classes as template arguments; Visual C ++ 2010已经支持使用本地类作为模板参数; support in other compilers may vary. 其他编译器的支持可能会有所不同。 I'd guess that any compiler that supports C++0x lambdas would also support the use of local classes as template arguments (this may not be entirely true, but it would make sense). 我猜任何支持C ++ 0x lambdas的编译器也支持使用本地类作为模板参数(这可能不完全正确,但它会有意义)。

I get a different error when I try to compile your code: 我尝试编译代码时遇到了不同的错误:

error: 'flip_all(__gnu_debug_def::map, std::allocator > >)::FlipFunctor' uses local type 'flip_all(__gnu_debug_def::map, std::allocator > >)::FlipFunctor' 错误:'flip_all(__ gnu_debug_def :: map,std :: allocator >>):: FlipFunctor'使用本地类型'flip_all(__ gnu_debug_def :: map,std :: allocator >>):: FlipFunctor'

That's actually to be expected, because a function local type (such as your FlipFunctor here) has internal linkage, and a template type must have external linkage. 这实际上是预期的,因为函数本地类型(例如此处的FlipFunctor)具有内部链接,并且模板类型必须具有外部链接。 Since the third parameter of std::for_each is a template, you cannot pass something of a function local type to it. 由于std :: for_each的第三个参数模板,因此您无法将函数本地类型传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM