为什么函数中定义的结构体不能用作std::for_each的函数呢?

时间:2022-11-10 18:03:12

The following code won't compile. The compiler complains about *no matching function for call to for_each*. Why is this so?

下面的代码无法编译。编译器会抱怨调用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());
}

When I move struct FlipFunctor before function flip_all, the code compiles.

当我在flip_all函数之前移动struct FlipFunctor时,代码就会编译。

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 <::pair const element> >, std:::_Rb_tree_iterator , locator _b0 >, stbbd: < bbbbbbd * Element:

2 个解决方案

#1


13  

std::for_each is a function template; one of the template parameters is the type of the function argument.

for_each是一个函数模板;模板参数之一是函数参数的类型。

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; 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).

Visual c++ 2010已经支持使用本地类作为模板参数;其他编译器中的支持可能有所不同。我猜想任何支持c++ 0x lambdas的编译器也会支持使用本地类作为模板参数(这可能不是完全正确的,但这是有意义的)。

#2


0  

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. Since the third parameter of std::for_each is a template, you cannot pass something of a function local type to it.

这实际上是可以预期的,因为函数本地类型(如此处的FlipFunctor)具有内部链接,模板类型必须具有外部链接。因为std::for_each的第三个参数是一个模板,所以不能将函数本地类型的东西传递给它。

#1


13  

std::for_each is a function template; one of the template parameters is the type of the function argument.

for_each是一个函数模板;模板参数之一是函数参数的类型。

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; 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).

Visual c++ 2010已经支持使用本地类作为模板参数;其他编译器中的支持可能有所不同。我猜想任何支持c++ 0x lambdas的编译器也会支持使用本地类作为模板参数(这可能不是完全正确的,但这是有意义的)。

#2


0  

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. Since the third parameter of std::for_each is a template, you cannot pass something of a function local type to it.

这实际上是可以预期的,因为函数本地类型(如此处的FlipFunctor)具有内部链接,模板类型必须具有外部链接。因为std::for_each的第三个参数是一个模板,所以不能将函数本地类型的东西传递给它。