本文共 1474 字,大约阅读时间需要 4 分钟。
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的。
译者注:关于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函数。
觉得本文有帮助?请分享给更多人。
更多更新文章,欢迎关注微信公众号【面向对象思考】
面向对象设计,面向对象编程,面向对象思考!
转载地址:https://blog.csdn.net/weixin_39934257/article/details/110712964 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!