我应该在头文件中定义静态内联方法吗? [英] Should I define static inline methods in header file?

查看:203
本文介绍了我应该在头文件中定义静态内联方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了如何通常最好不要在头文件中定义任何内容,因为为包含头文件的每个其他文件都会创建冗余副本。但是,在静态内联方法的情况下,似乎我必须在现场定义它(至少Visual Studio 2010不允许我这样做)。因此,如果我在头文件中编写一个接口,我不能在类定义或.cpp文件中定义静态内联方法。



我应该打扰使用静态内联方法吗?和一个相关的问题:我应该在头文件中定义任何方法或变量(常量)?



无论如何,奇怪的是,在我的C ++书中。



编辑:我读了关于静态内联方法的类似问题,但没有一个似乎直接解决这个问题。

解决方案


如何在头文件中添加函数定义?


这可以通过三种可能的方式实现:


  1. 标记函数 inline

  2. 使函数 static

  3. 将函数放在匿名命名空间中。

  4. #1 ie:标记函数 inline 是正确的方式,而不破坏 一个定义规则





    #2 & #3 每个翻译单元将包含它自己的函数版本,并且程序将包含函数的几个不同版本,从而导致生成的二进制文件的大小增加。

    ie:对于 static 函数 fun()& ; fun 在每个翻译单元中会有所不同,程序将包含 N 不同版本的函数。

    如果函数包含静态局部变量,则会有 N 个不同的静态局部变量,每个函数实例有一个。



    < blockquote>

    第一种方法如何避免此问题?


    c $ c> inline 函数具有外部链接。

    当标记一个函数 inline 时,函数将具有相同的地址在所有翻译单位。此外,内联函数体内定义的静态局部变量和字符串文字在翻译单元中被视为同一对象。

    简而言之,内联函数在所有转换单元中具有相同的地址。 p>


    在头文件中 static inline strong>


    static 关键字强制函数具有内部链接。

    定义为inline的每个函数实例都被视为一个单独的函数,每个实例都有自己的静态局部变量和字符串字面量的副本。因此,这类似于#2



    注意:
    标准要求用户程序中 inline 函数的所有定义在使用或调用函数的所有转换单元中,必须具有完全相同的定义。






    相关标准:



    +03标准



    3.2一个定义规则

    >



    每个程序都应包含每个非内联函数或在该程序中使用的对象的一个​​定义;无需诊断
    。定义可以在程序中显式显示,它可以在标准或用户定义的库中找到,或者(在适当的时候)被隐式定义(见12.1,12.4和12.8)。 应在每个使用它的翻译单元中定义内联函数。


    7.1 .2函数说明符

    第4章:


    应在其使用的每个翻译单位中定义,在每种情况下都应有完全相同的定义(3.2)。 [注意:在内联函数的定义出现在翻译单元中之前,可能会遇到对内联函数的调用。 ]如果具有外部链接的函数在一个翻译单元中被内联声明,则它应该在其出现的所有翻译单元中被内联地声明;不需要诊断。 具有外部链接的内联函数在所有转换单元中应具有相同的地址。 anextern内联函数中的静态局部变量始终引用相同的对象。



    I've read about how it is usually best not to define anything in header files because redundant copies are made for every other file that includes the header file. However, in the case of static inline methods, it appears that I have to define it on the spot (at least visual studio 2010 doesn't let me do that). So if I write an interface at the header file, I can't define the static inline method out side of the class definition or at the .cpp file.

    So, should I bother to use static inline methods at all? And a relate question: Should I even define anything method or variable in a header file (what about constants)?

    Anyway, strangely, it's not something that's covered in great detail in my C++ books.

    Edit: I read through similar questions about static inline methods but none of them seem to directly address this issue.

    解决方案

    How to add a function definition in header file?

    This can be achieved in 3 possible ways:

    1. Marking the function inline or
    2. Making the function static or
    3. Putting the functions in anonymous namespace.

    What is the correct way to do so?

    #1 i.e: Marking the function inline is the correct way to this without breaking the One Definition Rule.

    What is wrong with the other two appraoches?

    In both #2 & #3 each Translation Unit will contain it's own version of the function, and the program will contain several different versions of the function thus leading to a increase in size of the generated binary.
    i.e: For a static function fun(), &fun will be different in each translation unit, and the program will contain N different versions of the function.
    Also, If the function contains static local variables then there will be N different static local variables, one for each function instance.

    How the first approach avoids this problem?

    An inline function has external linkage.
    When you mark a function inline the function will have the same address in all translation units. Also, Static locals and string literals defined within the body of an inline function are treated as the same object across translation units.
    In short, a inline function will have the same address across all translation units.

    What is the deal with static inline function definitions in header?

    The static keyword forces the function to have a internal linkage.
    Each instance of function defined as inline is treated as a separate function and each instance has its own copy of static locals and string literals. Thus, this would be similar to #2.

    Note:
    The standard mandates that all definitions of inline function in an user program You must have the exact same definition in all translation units in which the function is used or called.


    Relevant Standerdese references:

    C++03 Standard

    3.2 One definition rule:
    Para 3:

    Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is used.

    7.1.2 Function specifiers
    Para 4:

    An inline function shall be defined in every translation unit in which it is used and shall have exactly the same definition in every case (3.2). [Note: a call to the inline function may be encountered before its definition appears in the translation unit. ] If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required. An inline function with external linkage shall have the same address in all translation units. A static local variable in anextern inline function always refers to the same object. A string literal in an extern inline function is the same object in different translation units.

    这篇关于我应该在头文件中定义静态内联方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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