Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c++ - Should I define static inline methods in header file?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...