什么时候应该为函数/方法写关键字'inline'?

什么时候应该为函数/方法写关键字'inline'?

问题描述:

我应该何时在C ++中为函数/方法写关键字 inline

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

,一些相关问题:


  • 我应该何时将关键字'inline' /方法在C ++中?

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

编译器什么时候不知道何时创建函数/方法'inline' b $ b

  • 如果应用程序多线程在为某个函数/方法写入'inline'时是否重要?

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


  • 哦,男人,我的宠物狗。

    Oh man, one of my pet peeves.

    inline 比指令更像是 static extern 内联您的功能。 extern static inline

    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 compilation units. Linker needs to make sure it doesn't accidentally use a statically defined variable/function from another compilation unit.

    extern - 在此编译单元中使用此变量/函数名,但不要在未定义的情况下提出投诉。

    extern - use this variable/function name in this compilation 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.

    链接器会将所有代码尝试使用一些extern符号。 inline - 此函数将在多个编译单元中定义,不必担心。链接器需要确保所有编译单元都使用变量/函数的单个实例。

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

    :通常,声明模板 inline 是无意义的,因为它们已经具有 inline 的链接语义。但是,显式模板的专门化和实例化 require 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 ++中为函数/方法写关键字'inline'?

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

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

    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 compilation 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 ++中为函数/方法写关键字'inline' / p>

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

    如果你认为你的代码运行的更快,如果编译器内联它,不要内联。

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


    编译器何时不知道何时创建函数/方法inline?

    When will the 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中的内联,使用 __属性__((noinline)),在Visual Studio中,使用 __ declspec(noinline)

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


    如果一个应用程序在为函数/方法写入'inline' b $ b

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

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

    Multithreading doesn't affect inlining in any way.