C++ 我应该在头文件中定义静态内联方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10847176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:31:45  来源:igfitidea点击:

Should I define static inline methods in header file?

c++staticinlineheader-files

提问by Some Newbie

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.

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

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.

无论如何,奇怪的是,我的 C++ 书籍中并没有详细介绍它。

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

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

回答by Alok Save

How to add a function definition in header file?

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

This can be achieved in 3 possible ways:

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

  1. Marking the function inlineor
  2. Making the function staticor
  3. Putting the functions in anonymous namespace.
  1. 标记函数inline
  2. 使函数static
  3. 将函数放在匿名命名空间中。

What is the correct way to do so?

这样做的正确方法是什么?

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

#1即:在inline不破坏单一定义规则的情况下,标记函数是正确的方法。

What is wrong with the other two appraoches?

其他两种方法有什么问题?

In both #2& #3each 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 staticfunction fun(), &funwill be different in each translation unit, and the program will contain Ndifferent versions of the function.
Also, If the function contains static local variables then there will be Ndifferent static local variables, one for each function instance.

在两者中#2#3每个翻译单元都将包含它自己的函数版本,并且程序将包含函数的几个不同版本,从而导致生成的二进制文件的大小增加。
即:对于一个static函数fun()&fun每个翻译单元都会N不同,程序会包含不同版本的函数。
此外,如果函数包含静态局部变量,那么将有N不同的静态局部变量,每个函数实例一个。

How the first approach avoids this problem?

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

An inlinefunction has external linkage.
When you mark a function inlinethe 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.

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

What is the deal with static inlinefunction definitions in header?

static inline头文件中的函数定义是怎么处理的?

The statickeyword 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.

static关键字强制功能有一个内部链接。
定义为内联函数的每个实例都被视为一个单独的函数,并且每个实例都有自己的静态局部变量和字符串文字的副本。因此,这将类似于#2

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

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



Relevant Standerdese references:

相关标准参考文献:

C++03 Standard

C++03标准

3.2 One definition rule:
Para 3:

3.2 一个定义规则:
第 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.

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

7.1.2 Function specifiers
Para 4:

7.1.2 功能说明符
第 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.

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

回答by iammilind

(1) I can't define the static inline method out side of the class definition or at the .cpp file.

(1) 我无法在类定义之外或在 .cpp 文件中定义静态内联方法。

You can define a static inlinemethod outside the class within the header file. Demo. You cannot define them in .cpp file.

您可以static inline在头文件中的类之外定义一个方法。演示。您不能在 .cpp 文件中定义它们。

(2) should I bother to use static inline methods at all

(2) 我应该费心使用静态内联方法吗

I would say, they can be easily avoided. If you need to see the body in the header file for your own purpose, then only make them inline.

我想说,它们很容易避免。如果您出于自己的目的需要查看头文件中的正文,则只需 make them inline

(3) Should I even define anything method or variable in a header file (what about constants)

(3) 我是否应该在头文件中定义任何方法或变量(常量呢)

  1. Inside the class body, you can define static constdata of integral type.
  2. staticmethods can be defined inside the class body
  3. static inlinemethods can be defined inside class body or outside the class body within the header file
  4. staticdata members must be defined in single .cppfile to adhere with One Definition Rule
  1. 在类体内,您可以定义static const整数类型的数据。
  2. static方法可以在类体内定义
  3. static inline方法可以定义在类体内部或类体外部的头文件中
  4. static数据成员必须在单个.cpp文件中定义以遵守一个定义规则