C++核心准则边译边学-F.5 如果函数非常小而且时间敏感,将其定义为inline

网友投稿 477 2022-10-25

C++核心准则边译边学-F.5 如果函数非常小而且时间敏感,将其定义为inline

C++核心准则边译边学-F.5 如果函数非常小而且时间敏感,将其定义为inline

F.5: If a function is very small and time-critical, declare it ​​inline(如果函数非常小而且时间敏感,将其定义为inline)​​

Reason(原因)

Some optimizers are good at inlining without hints from the programmer, but don't rely on it. Measure! Over the last 40 years or so, we have been promised compilers that can inline better than humans without hints from humans. We are still waiting. Specifying ​​inline​​ encourages the compiler to do a better job.

有些优化程序擅长在程序员没有提示的时候就行inline处理,但是不要依靠这一点。算一下!差不多有四十年左右了,我们被许诺编译器可以在没有得到任何提示的情况下可以比人进行更好的inline处理。我们还在等。还是定义inline以便让编译器更好地工作吧。

Example(示例

inline string cat(const string& s, const string& s2) { return s + s2; }

Exception(例外)

Do not put an ​​inline​​ function in what is meant to be a stable interface unless you are certain that it will not change. An inline function is part of the ABI.

不要将inline函数放进本来希望稳定的接口中,除非你确定它不会改变。inline函数也是ABI的一部分。

译者注:inline可能会随着实现方式的变化而隐式或显示的发生变化。

Note(注意)

​​constexpr​​​ implies ​​inline​​.

constexpr也包含inline的意思。

Note(注意)

Member functions defined in-class are ​​inline​​ by default.

译者注:关于inline函数,在《effective C++》中有详细的描述。

Exception(例外)

Template functions (incl. template member functions) are normally defined in headers and therefore inline.

模板函数通常定义在头文件中,因此也是inline。

Enforcement(实施建议)

Flag ​​inline​​ functions that are more than three statements and could have been declared out of line (such as class member functions).

标记那些超过3条语句且应该已经单独定义(例如类成员函数)的inline函数。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C++核心准则边译边学-I.30 封装必要的违反
下一篇:Repulsive Grizzly是一个应用层DoS测试框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~