Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I define static inline methods in header file?

Tags:

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.

like image 501
Some Newbie Avatar asked Jun 01 '12 08:06

Some Newbie


1 Answers

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.

like image 181
Alok Save Avatar answered Oct 21 '22 17:10

Alok Save