为什么我不能将函数中定义的函子传递给另一个函数? [英] Why I cannot pass a functor defined in a function to another function?
问题描述
我发现函数可以用来模拟在这样的函数中定义一个函数。
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;
}
但是如果我将它传递给外部函数,如 std :: transform 就像下面的例子,我有一个编译错误,说:错误:没有匹配的函数调用'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;
}
所以我把定义放在 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;
}
4.8.2,它是一个C ++ 11编译器。
然而,C ++ 03编译器会阻碍用本地类型实例化模板,因为它不是在C ++ 03中支持。
一个解决方案,如果这是问题的根本原因,那么就是使用更新的编译器版本或其他编译器。 / p>
另一个解决方案是利用它,即使在C ++ 03中,您也可以通过使用一个静态成员函数的本地类(在C ++ 11中,你也可以通过使用lambda表达式)。
但是,除了处理这样的问题,函子相对于真实函数的一般性能优点,即具有类的对象而不仅仅是函数指针,并且相关 operator()
作为 inline
,编译器可以优化得更好,因为它知道函数的实现。
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&)’
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.
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.
A C++03 compiler would however balk at instantiating a template with a local type, since that was not supported in 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).
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.
这篇关于为什么我不能将函数中定义的函子传递给另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!