考虑这个简单的程序:
#include <iostream>
void length(void){
std::cout<<"My length is void"<<std::endl;
}
void width(void){
std::cout<<"My width is void"<<std::endl;
}
int main(void){
std::cout<<"The program length is: "<<length<<std::endl;
std::cout<<"The program width is: "<<width<<std::endl;
}
这个程序的输出是:
The program length is: 1
The program width is: 1
程序打印出来的数字是多少,基本上对 C++ 没有太多了解,这对我来说看起来更像 pythonic 语法,看起来好像应该打印函数地址。我这样说是因为,这个问题最初是在练习一些非常基本的 openGL 程序时出现的,
回调已在 GLUT 中注册,内容如下:
void line(void){
//some code for line program ...
}
int main(int argc, char** argv){
//some more code;
glutDisplayFunc(line);
glutMainLoop();
}
看起来好像我们传递的是函数的地址,但是,从上面的程序中可以清楚地看出这不是地址,函数指针的语法有点不同,如果是这样,如何这个函数是否正在注册回调?我们要传递给 glutDisplayFunc
的是什么?
并且,因为我**想要注册一个传递参数的函数,所以我搜索了一个 C++ 类比的 python lambda 函数,并找到了类似的 lambda 函数,但它没有成功:**
#include <iostream>
void line(int a, int b){
//some code that does some plotting in the display window
}
int main(int argc, char** argv){
auto callback = [](void)->void{
line(a,b);
};
glutDisplayFunc(callback);
glutMainLoop();
}
这完全失败了,显示的错误是:
no suitable conversion function from "lambda []void ()->void" to "void (*)()" exists
这是使用我的 python 类比,但是这个问题的解决方案是什么?混淆的主要部分以粗体突出显示。
最佳答案
It's almost seeming as if we're passing the address of the function, but, clearly from the above program this is not an address
你把事情弄反了。这不是 glutDisplayFunc
那是在做一件奇怪的神奇事情;这是std::cout
.
是的,函数名将转换为函数指针。 operator<<
的组合iostream 的重载和 C++ 重载解析规则最终导致 <<
将函数视为 bool 值(是的,真的)。因此,您的 << length
相当于做 << true
.
如果你想要地址,你可以将函数指针转换为 void*
在输出之前:<< reinterpret_cast<void*>(&length)
或更简洁的 << (void*)&length
(虽然从技术上讲这只是 C++ 有条件地支持,但几乎每个真正的编译器都允许这样做)。
最后,在 C++ 中,lambda 不是函数;它们是对象。您不能将对象传递给需要函数指针的对象。您可以将无捕获的 lambda 转换为函数指针,但不能为捕获值的 lambda 执行此操作。
关于c++ - 函数名称表示什么以及更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52893125/