C ++类中的内联规则是什么? [英] What are the inlining rules within C++ classes?

查看:113
本文介绍了C ++类中的内联规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我在很久以前读到的东西,似乎如果你想在编译阶段内联类成员函数,函数必须在类声明块中定义。



但这有一个细节泄漏的缺点。 IMHO,其他程序员只能在打开.h文件时看到类接口。



现代C ++中第一条语句是否仍然成立?有没有办法强制内联的函数声明,最好是在另一个文件中?



一般来说,更好的方法是在类声明块中保留短成员函数, ?

解决方案


似乎如果你想在编译阶段内联类成员函数,该函数必须在类声明块中定义。


这不是真的。在类定义内定义的函数隐式标记为 inline 。但是你不需要在类中定义函数为 inline ,你可以显式请求它:

  struct X {
void f();
};
inline void f(){}

inline <另一方面,code>关键字并不意味着该函数将被内联 ,而是可以在多个翻译单元中定义如果多个翻译单元包含包含该定义的相同标题,则链接程序不会失败,并出现多重定义错误

现在,实际上 ,编译器可以决定是否内联任何函数,无论函数是否声明为 inline 定义(它将内联的代码),这就是为什么一般来说意在内联的函数应该在头文件中定义的原因(在类定义中或者标记为 inline outside。



此外,较新的工具链可以执行整个程序优化或其他链接时间优化链接器还可以决定函数应该内联。在这种情况下,函数定义不需要在调用站点处可见,因此可以在.cpp文件中定义。但是如果你真的想让函数内联,最好不要依赖这个功能,只需在标题中定义函数。


From what I read somewhere long time ago, it seems that if you want class member function to be inlined during the compilation phase, the function has to be defined inside class declaration block.

But this has a downside of a detail leak. IMHO, other programmers should only see class interface when opening .h file.

Is the first statement still true in modern C++, was it ever? Is there a way to force inlining for functions that are declared, preferably in another file altogether?

Is it generally better to keep short member functions inside class declaration block, or not?

解决方案

It seems that if you want class member function to be inlined during the compilation phase, the function has to be defined inside class declaration block.

That is not really true. A function that is defined inside the class definition is implicitly marked as inline. But you don't need to defined the function inside the class for it to be inline, you can explicitly request it:

struct X {
   void f();
};
inline void f() {}

The inline keyword on the other hand, does not mean that the function will be inlined, but rather that it can be defined in multiple translation units, that is, if multiple translation units include the same header that contains that definition, the linker will not fail with a multiple definition error.

Now, on actual inlining, the compiler can decide to inline or not any function, regardless of whether the function is declared as inline provided that it sees the definition of that function (the code that it will inline), which is the reason why in general functions that are meant to be inlined should be defined in the header (either inside the class definition or marked inline outside.

Additionally, newer toolchains can perform whole program optimization or other link time optimizations, by which the linker can also decide that a function should be inlined. In this case, the function definition needs not be visible at the call site, so it could be defined inside the .cpp file. But if you really want the function to be inlined it is better not to depend on this feature and just define the function in the header.

这篇关于C ++类中的内联规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆