简体   繁体   English

为什么我不能将函数中定义的函子传递给另一个函数?

[英]Why I cannot pass a functor defined in a function to another function?

I found the functor can be used to simulate defining a function within a function like this 我发现仿函数可以用来模拟在这样的函数中定义一个函数

using namespace std;
int main(int argc, char* argv[])
{
    struct MYINC {
        int operator()(int a) { return a+1; }
    } myinc;
    vector<int> vec;
    for (int i = 0; i < 10; i++) vec.push_back(myinc(i));
    return 0;
}

But If I passed it to an outside function, such as std::transform like the following example, I've got a compiling error saying error: no matching function for call to 'transform(std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator, main(int, char**)::MYINC&)' 但是,如果我将它传递给外部函数,如std :: transform ,如下例所示,我有一个编译错误说error: no matching function for call to 'transform(std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator, main(int, char**)::MYINC&)'

using namespace std;
int main(int argc, char* argv[])
{
    struct MYINC{
        int operator()(int a) { return a+1; }
    } myinc;
    vector<int> vec;
    for (int i = 0; i < 10; i++) vec.push_back(i);
    transform(vec.begin(), vec.end(), vec.begin(), myinc);
    return 0;
}

So I put the definition outside the main function and all is OK now. 所以我把定义放在main函数之外,现在一切都好了。

using namespace std;
struct MYINC{
    int operator()(int a) { return a+1; }
} myinc;
int main(int argc, char* argv[])
{
    vector<int> vec;
    for (int i = 0; i < 10; i++) vec.push_back(i);
    transform(vec.begin(), vec.end(), vec.begin(), myinc);
    return 0;
}

Both versions compile fine with g++ 4.8.2, which is a C++11 compiler. 两个版本都可以使用g ++ 4.8.2编译,这是一个C ++ 11编译器。

A C++03 compiler would however balk at instantiating a template with a local type, since that was not supported in C++03. 但是,C ++ 03编译器不会实例化具有本地类型的模板,因为C ++ 03不支持这种模板。

One solution, if that is indeed the root cause of the problem, is then to use a more recent compiler version or other compiler. 一个解决方案,如果这确实是问题的根本原因,那么就是使用更新的编译器版本或其他编译器。

Another solution is to leverage that even in C++03 you can define a "real" function locally, by making it a static member function of a local class (in C++11 you can also do that by using a lambda expression). 另一个解决方案是利用它,即使在C ++ 03中,你可以在本地定义一个“真正的”函数,使它成为本地类的静态成员函数(在C ++ 11中,你也可以通过使用lambda表达式来实现) 。

However, except for dealing with such a problem, the functor has a general performance advantage over the "real" function, namely that with an object of a class instead of just a function pointer, and with the relevant operator() as inline , the compiler can optimize much better, because it knows the function implementation. 然而,除了处理这样的问题之外,仿函数与“真实”函数相比具有一般性能优势,即具有类的对象而不仅仅是函数指针,并且相关的operator()inline函数,编译器可以更好地优化,因为它知道函数实现。

For reasons that aren't entirely clear to me there is (was) a well-known and quite annoying limitation in C++ 03 on which types you can use to instantiate a template. 由于我不完全清楚的原因,在C ++ 03中有一个众所周知且非常恼人的限制,您可以使用它来实例化模板。

Locally defined classes could not be used as parameters with templates, just because. 本地定义的类不能用作模板的参数,只是因为。

This by the way made quite difficult to make any decent use of the <algorithm> library because you could not keep the context of your code local, being forced instead to place all functors, comparators and the like at namespace level, making up funny names for them and placing them far from the point of use. 顺便说一句,很难对<algorithm>库进行任何合理的使用,因为你无法将代码的上下文保持在本地,而是强制将所有的函子,比较器等放在命名空间级别,组成有趣的名字对于他们而言,他们远离使用点。

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

相关问题 模板函数+函子参数,为什么函子没有内联? - Template function + functor argument, why the functor is not inlined? C ++将函数传递给functor - C++ pass function into functor 如何创建可以传递函数或函子的方法? - How to create a method to which I could either pass function or a functor? 为什么不能在btAlignedObjectArray:quicksort中使用仿函数作为比较器函数? - Why can't I use a functor as the comparator function in btAlignedObjectArray:quicksort? 将模板仿函数传递给模板std :: function - Pass template functor to a template std::function 为什么函数内定义的结构不能用作std :: for_each的函子? - Why can't a struct defined inside a function be used as functor to std::for_each? 为什么 C++ std::function 通过值而不是通用引用传递函子? - Why C++ std::function pass the functor by value instead of universal reference? 为什么我不能将 ifstream 变量作为参数传递给 function? - Why cannot I pass an ifstream variable to a function as a parameter? C++ 如何将成员函数传递给函子参数? - C++ How do you pass a member function into a functor parameter? 使用STL算法,传递函数指针或函子是否更好? - Using STL algorithms, is it better to pass a function pointer or a functor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM