简体   繁体   English

C ++函数类型?

[英]C++ Function Type?

I'm new to, and learning C++ (know a lot of Java) and the following code confuses me... 我是新手,学习C ++(知道很多Java),以下代码让我感到困惑......

在此输入图像描述

I know this code fragment is dealing with a pointer-to-function (it's a callback, that makes sense) but what is throwing me off is the argument between the return type and the function name. 我知道这个代码片段正在处理指向函数的指针(它是一个回调,这是有道理的)但是什么让我失望的是返回类型和函数名之间的参数。 What the bloody hell is that? 这到底是什么血腥的?

It looks like a type of function, but I have never heard of that and even after searching and reading about pointer-to-functions I was not able to find anything mentioning that functions could have a type. 它看起来像一种函数,但我从来没有听说过,甚至在搜索和读取函数指针后,我也无法找到任何提及函数可能有类型的东西。

If this is true, how does one define a function type? 如果是这样,那么如何定义函数类型?

Thanks, -Cody 谢谢,-Cody

GLFWCALL is not a type, it's a macro which is expanded to a calling convention specific to the platform, or an empty string. GLFWCALL不是一个类型,它是一个扩展为特定于平台的调用约定的宏,或一个空字符串。 Here's a trimmed fragment of glfw.h: 这是glfw.h的修剪片段:

#if defined(_WIN32) && defined(GLFW_BUILD_DLL)
 #define GLFWCALL     __stdcall
#elif defined(_WIN32) && defined(GLFW_DLL)
 #define GLFWCALL     __stdcall
#else
 /* We are either building/calling a static lib or we are non-win32 */
 #define GLFWCALL
#endif

Using a correct calling convention is important on x86/win32, since some of them expect the stack to be cleaned by callee and others by the caller. 在x86 / win32上使用正确的调用约定非常重要,因为有些调用约会希望调用者可以通过被调用者清理堆栈。 There can also be differences in the order of passing the arguments. 传递参数的顺序也可能存在差异。

On Windows, GLFWCALL is a macro for __stdcall , and on other platforms, it's a macro for nothing. 在Windows上, GLFWCALL__stdcall的宏,而在其他平台上,它是一个什么都GLFWCALL的宏。

__stdcall implements a particular calling convention, and is a compiler extension on top of normal C or C++. __stdcall实现了一个特定的调用约定,并且是普通C或C ++之上的编译器扩展。

Macros are pieces of code that do replacements on your code before the lexer and parser of your compiler interact with them. 宏是在编译器的词法分析器和解析器与它们交互之前替换代码的代码片段。

The GLFWCALL is a macro that can expand to a calling convention if one is needed. GLFWCALL是一个宏,如果需要,可以扩展为调用约定 Because this function will be called by external code, it has to use the calling convention that external code expects. 因为这个函数将被外部代码调用,所以它必须使用外部代码所期望的调用约定。 For example, if the function puts its return value on the stack and the external code expects it in a register, boom. 例如,如果函数将其返回值放在堆栈上,而外部代码将它放在寄存器中,则繁荣。

The question marked part of the function signature is a preprocessor macro that is defined somewhere else in the header. 标记为函数签名的一部分的问题是预处理器宏,该宏在头部的其他位置定义。 Certain features on certain platforms have extra requirements. 某些平台上的某些功能有额外的要求。

For example functions in DLL files on the Windows platform often make use of the __declspec(dllexport) modifier but when the same header is included in a user's project they need to use __declspec(dllimport). 例如,Windows平台上的DLL文件中的函数通常使用__declspec(dllexport)修饰符,但是当用户的项目中包含相同的头时,他们需要使用__declspec(dllimport)。 Using a preprocessor macro for that purpose means they can just use that macro on all relevant functions and simply define the macro differently when compiling their own DLL or a user's DLL and on platforms where __declspec is irrelevant it can be defined to nothing. 为此目的使用预处理器宏意味着他们可以在所有相关函数上使用该宏,并且在编译自己的DLL或用户DLL时简单地定义宏,并且在__declspec不相关的平台上可以将其定义为空。 There are many other reasons for macros like that one. 像这样的宏还有很多其他原因。

In this particular case you can effectively pretend that macro is blank and ignore it entirely. 在这种特殊情况下,您可以有效地假装宏为空并完全忽略它。

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

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