我什么时候应该为函数/方法编写关键字“内联"? [英] When should I write the keyword 'inline' for a function/method?

查看:38
本文介绍了我什么时候应该为函数/方法编写关键字“内联"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该什么时候为 C++ 中的函数/方法编写关键字 inline?

When should I write the keyword inline for a function/method in C++?

看到一些答案后,一些相关的问题:

After seeing some answers, some related questions:

  • 我什么时候应该为 C++ 中的函数/方法编写关键字内联"?

  • When should I not write the keyword 'inline' for a function/method in C++?

编译器何时不知道何时使函数/方法内联"?

When will the compiler not know when to make a function/method 'inline'?

当为函数/方法编写内联"时,应用程序是否多线程有关系吗?

Does it matter if an application is multithreaded when one writes 'inline' for a function/method?

推荐答案

哦,天哪,我最讨厌的一个.

Oh man, one of my pet peeves.

inline 更像是 staticextern 而不是告诉编译器内联函数的指令.externstaticinline 是链接指令,几乎只由链接器使用,而不是编译器.

inline is more like static or extern than a directive telling the compiler to inline your functions. extern, static, inline are linkage directives, used almost exclusively by the linker, not the compiler.

据说 inline 向编译器暗示您认为该函数应该被内联.这在 1998 年可能是正确的,但十年后编译器不需要这样的提示.更不用说人类在优化代码时通常是错误的,因此大多数编译器都会直接忽略提示".

It is said that inline hints to the compiler that you think the function should be inlined. That may have been true in 1998, but a decade later the compiler needs no such hints. Not to mention humans are usually wrong when it comes to optimizing code, so most compilers flat out ignore the 'hint'.

  • static - 变量/函数名称不能在其他翻译单元中使用.链接器需要确保它不会意外使用来自另一个翻译单元的静态定义的变量/函数.

  • static - the variable/function name cannot be used in other translation units. Linker needs to make sure it doesn't accidentally use a statically defined variable/function from another translation unit.

extern - 在此翻译单元中使用此变量/函数名称,但如果未定义,请不要抱怨.链接器将对其进行整理并确保所有尝试使用某个外部符号的代码都有其地址.

extern - use this variable/function name in this translation unit but don't complain if it isn't defined. The linker will sort it out and make sure all the code that tried to use some extern symbol has its address.

inline - 这个函数会在多个翻译单元中定义,不用担心.链接器需要确保所有翻译单元都使用变量/函数的单个实例.

inline - this function will be defined in multiple translation units, don't worry about it. The linker needs to make sure all translation units use a single instance of the variable/function.

注意:通常,声明模板inline 是没有意义的,因为它们已经具有inline 的链接语义.但是,模板的显式特化和实例化需要使用inline.

Note: Generally, declaring templates inline is pointless, as they have the linkage semantics of inline already. However, explicit specialization and instantiation of templates require inline to be used.

您问题的具体答案:

我应该什么时候为 C++ 中的函数/方法编写关键字内联"?

When should I write the keyword 'inline' for a function/method in C++?

仅当您希望在标题中定义函数时.更准确地说,只有当函数的定义可以出现在多个翻译单元中时.在头文件中定义小的(如在一个 liner 中)函数是一个好主意,因为它在优化代码的同时为编译器提供了更多的信息.它还增加了编译时间.

Only when you want the function to be defined in a header. More exactly only when the function's definition can show up in multiple translation units. It's a good idea to define small (as in one liner) functions in the header file as it gives the compiler more information to work with while optimizing your code. It also increases compilation time.

我什么时候不应该为 C++ 中的函数/方法编写关键字内联"?

When should I not write the keyword 'inline' for a function/method in C++?

不要仅仅因为您认为如果编译器内联您的代码会运行得更快而添加内联.

Don't add inline just because you think your code will run faster if the compiler inlines it.

编译器何时不知道何时使函数/方法内联"?

When will the compiler not know when to make a function/method 'inline'?

一般来说,编译器会比你做得更好.但是,如果没有函数定义,编译器就没有内联代码的选项.在最大程度优化的代码中,无论您是否要求,通常所有 private 方法都会内联.

Generally, the compiler will be able to do this better than you. However, the compiler doesn't have the option to inline code if it doesn't have the function definition. In maximally optimized code usually all private methods are inlined whether you ask for it or not.

为了防止在 GCC 中内联,请使用 __attribute__(( noinline )),并在 Visual Studio 中使用 __declspec(noinline).

As an aside to prevent inlining in GCC, use __attribute__(( noinline )), and in Visual Studio, use __declspec(noinline).

当为函数/方法编写内联"时,应用程序是否是多线程的重要吗?

Does it matter if an application is multithreaded when one writes 'inline' for a function/method?

多线程不会以任何方式影响内联.

Multithreading doesn't affect inlining in any way.

这篇关于我什么时候应该为函数/方法编写关键字“内联"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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