我试图完全理解内联的作用,但当涉及到构造函数时我感到困惑。到目前为止,我了解到内联函数会将函数放在调用它的位置:
class X {
public:
X() {}
inline void Y() { std::cout << "hello" << std::endl; }
};
int main() {
X x;
x.Y; //so instead of this it will be: "std::cout << "hello" << std::endl;"
}
Run Code Online (Sandbox Code Playgroud)
但这会做什么(如何替换它?):
class X {
public:
//or inline if the compilers feeling friendly for this example
__forceinline X() {}
}
Run Code Online (Sandbox Code Playgroud)
的含义inline只是通知编译器所讨论的函数将在此翻译以及可能的其他翻译单元中定义。编译器使用此信息有两个目的:
内联函数的类型没有区别。即是否是构造函数、成员函数、普通函数等。
顺便说一句,在类定义中定义的成员函数是隐式声明的inline。无需明确提及。__forecedinline似乎是一个编译器扩展,假设它按照它所说的去做可能是一个坏主意,因为编译器比人类更擅长决定应该内联什么。
| 归档时间: |
|
| 查看次数: |
1536 次 |
| 最近记录: |