inline 这个函数,有意义吗?

inline this function, does make sense?

我有下一个成员函数:

template <typename T>
inline T Foo::Read(const DWORD addr) const                          // Passing by value.
{
    T buffer;
    ReadProcessMemory(m_processHandle, (LPVOID)addr, &buffer, sizeof(T), NULL);
    return buffer;
}

如果我没记错的话,当编译器内联一个函数时,它会避免调用该函数并将被调用函数的代码放入调用函数中。

所以在调用函数中(假设一个整数 return 类型)我想要这样的东西:

ReadProcessMemory(m_processHandle, (LPVOID)addr, &bufferOfTheCaller, sizeof(int), NULL);

关于这个我有三个问题:

1) 我们从函数中 return 得到的变量会发生什么? 变量buffer的声明不是在运行时间内执行的吗?

2) 在这种情况下 ReadProcessMemory 是 WinAPI 中的一个巨大函数,编译器是否仍然能够内联该函数?

3) 将成员函数定义在class定义内部,和用关键字inline[=32声明有什么区别=] 在 class 定义之外?如果我想使用 inline 关键字,我是否必须将内联函数放在同一个文件中 .h 或?

If I'm not wrong, when the compiler inlines a function, it avoids calling the function and put the code of the called function into the caller function.

是的,但是 inline 与此关系不大。

1) What would happen with the variable that we return from the function? Isn't the declaration of the variable buffer performed in run time?

编译器,无论是否内联,都必须为 buffer 保留一些 space,通常在堆栈上。

也就是说没有bufferOfTheCaller。如果你的函数是内联的,buffer 将在调用者的栈帧中;否则,它将被放入被调用者的堆栈帧中。

2) In this case ReadProcessMemory is a huge function from the WinAPI, should the compiler still able to inline this function?

不管 ReadProcessMemory 的实现有多大,你的代码只是对它执行一个函数调用,这是很小的。优化编译器可能会内联您的函数。

3) What is the difference between leaving the member function defined inside the class definition and declare it with the keyword inline outside the class definition?

没有区别。

If I want to use inline keyword, Do I have to put the inlined function in the same file .h or?

inline 关键字与内联无关。如果您想将函数的定义放在头文件中,您可能需要 inline 以防止重新定义错误。

What would happen with the variable that we return from the function?

它将被销毁,因为 return 值被函数调用表达式丢弃。

Isn't the declaration of the variable buffer performed in run time?

声明发生在编译时。

In this case ReadProcessMemory is a huge function from the WinAPI, should the compiler still able to inline this function?

如果编译器知道函数的定义,那么它可以扩展它内联。是否应该或是否会这样做取决于许多因素。函数的大小是一种启发式方法,可能会影响编译器做出的选择。

What is the difference between leaving the member function defined inside the class definition and declare it with the keyword inline outside the class definition?

在一种情况下,定义在 class 内部,而在另一种情况下,它在外部。其他没有区别。

If I want to use inline keyword, Do I have to put the inlined function in the same file .h or?

  • 如果您想内联定义一个成员函数,但又想在 class 定义之外定义它,那么您必须在 class 定义内声明函数内联 - 函数模板除外,这是隐式内联的。
  • 如果要在class定义内定义成员函数,则不需要显式声明内联;会这么含蓄。
  • 如果你不想定义内联函数,那么你必须在class定义之外定义函数并且不能使用inline关键字。

重要的是要注意 inline 关键字与函数的内联调用无关。它所做的只是允许在多个翻译单元中定义函数,只要所有定义都相同。

inline 关键字的作用是让您在头文件中定义一个函数,该函数将包含在多个翻译单元中。这可能会给编译器一个更好的机会来内联调用该函数,因为它具有在多个翻译单元中可用的完整定义,但这不是命令,甚至不是您希望编译器这样做的提示。编译器将自行决定是否应该内联对任何给定函数的调用。如果您确实想强制内联对函数的调用,则可以使用特定于编译器的扩展,但这不是 inline 关键字的作用。


顺便说一句,我要补充一点,内联函数调用的编译器根本不会改变 C++ 的规则。它不是文本替换,如预处理器宏。编译器将弄清楚如何将被调用函数的逻辑插入到调用者中。那时,C++ 变量之类的东西并不真正存在。如果您的来电看起来像这样:

int someValue = myFoo.Read(someAddress);

然后编译器可以轻松地将其转换为如下内容:

int someValue;
ReadProcessMemory(myFoo.m_processHandle, (LPVOID)someAddress, &someValue, sizeof(int), NULL);

由于假设规则。这两个片段都会导致相同的可观察行为,因此编译器可以在它们之间自由转换。