23

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.

phimuemue
  • 34,669
  • 9
  • 84
  • 115
Some Newbie
  • 1,059
  • 3
  • 14
  • 33
  • 2
    If you don't put the definitions in the header file, then the compiler can't inline it... –  Jun 01 '12 at 08:52
  • 1
    If you want something inlined, that _always_ involves redundant copies. – leftaroundabout Jun 01 '12 at 08:53
  • 1
    The problem with defining things in header files that are *not* either inline functions, or have internal linkage, or both, isn't that the copies are redundant. The problem is that the copies are harmful, you get conflicting symbols and your object files won't link together. However, in the case of static inline functions the copies aren't harmful, they're the whole purpose of making the function static inline. – Steve Jessop Jun 01 '12 at 09:18

2 Answers2

45

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I am a bit confused. Steve Jessop above seemed to suggest the copies of static inline functions aren't harmful, but it appears you are suggesting otherwise. Also, leftaroundabout suggested inline methods (presumably only definable within header and not cpp) always involve redundant copies, whereas you appear to be suggesting otherwise. – Some Newbie Jun 01 '12 at 09:54
  • @SomeNewbie: I think Steve's comment of *harmful* was w.r.t linking problems, With `static inline`, there won't be any linking problems which is what my answer says as well.But the problem is that there will be N instances of the function and each instance will have its own copy of static locals & string literals, Should you use it depends on whether you want this behavior. – Alok Save Jun 01 '12 at 10:10
  • @SomeNewbie: There are no redundant copies when you mark a function `inline`, a function will have the same address across all the translation units.But it is users responsibility to ensure that function has same definition across all translation units. – Alok Save Jun 01 '12 at 10:14
  • @Als: yes, you're right what I meant. I'm not treating the code bloat as "harmful", because unlike the linking problems it doesn't stop the program compiling. Bloat is a separate argument between you and your compiler. Normally it doesn't matter -- if a function actually gets inlined then there are multiple copies of (most of) it lying around anyway. Sometimes it matters, and using just `inline` is a precaution against that. The disadvantage of `inline` functions (as you note) is that the definition must be identical in all TUs: a stronger compatibility restriction than `static` functions. – Steve Jessop Jun 01 '12 at 11:05
  • Let me try to get this straight... for a header with a defined function f(): - f() has one TU per #include if a function is static inline or simply not inline - f() has only 1 TU for all #include if a function is inline but not static inline - When a function has multiple TU's, static locals are independent. (I am not sure about the significance of string literal duplication, since it should be the same issue as static local duplication right?) P.S. Can newlines be used for "responses" in here? I've tried
    as suggested but it never works.
    – Some Newbie Jun 01 '12 at 21:38
  • @SomeNewbie: Your summarization is correct. string literals have internal linkage. A string literal has the type ` static const char []` so yes the deal with them is same as with static variables.Just mentioned it for clarity, there is usually confusion about string literals linkage. – Alok Save Jun 02 '12 at 05:08
  • *"Marking the function inline is the correct way to this without breaking the One Definition Rule."* - ***Plus One***. I *did not* realize ODR applied to functions as well. Also see [static inline functions in headers and -Wunused-function](http://lists.llvm.org/pipermail/cfe-users/2016-January/000854.html) of the CFE-Users mailing list. – jww Jan 28 '16 at 22:04
7

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

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

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

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.

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

  1. Inside the class body, you can define static const data of integral type.
  2. static methods can be defined inside the class body
  3. static inline methods can be defined inside class body or outside the class body within the header file
  4. static data members must be defined in single .cpp file to adhere with One Definition Rule
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • *"If you need to see the body in the header file for your own purpose, then only make them inline"*, How about `template` functions? – Alok Save Jun 01 '12 at 09:54
  • *"Inside the class body, you can define static const data of integral type."* [This has changed in C++11](http://stackoverflow.com/questions/9656941/why-i-cant-initialize-non-const-static-member-or-static-array-in-class/9657064#9657064) – Alok Save Jun 01 '12 at 10:20
  • @Als, `template`s are `inline` by default, failing to define them in a header file results in linking error. I think OP hasn't intended them. I haven't covered C++11 as it's not tagged. The question seems to be in more from a reference from a typical C++ book. – iammilind Jun 01 '12 at 10:27
  • Templates are **not** `inline` by default.If you want a template function to act as `inline` you have to mark it `inline`explicitly. Functions defined inside the class definition are `inline` by default. – Alok Save Jun 01 '12 at 11:34
  • @Als, no that's incorrect. `template` functions are `inline` by default. Mentioning `inline` with a `template` function is **redundant**. We are talking about the guaranteed effect of `inline` keyword, which ensures that only 1 definition is generated across all the translation units. [Here is the thread](http://stackoverflow.com/questions/10535667/does-it-make-any-sense-to-use-inline-keyword-with-templates), if a `template` is completely specialized then only `inline` keyword makes sense. – iammilind Jun 01 '12 at 11:40
  • There is a difference between getting the same observable behavior as with `inline` and specifying a `inline`explicitly, The statement *template functions are inline by default* is Incorrect.You should read the marked thread more closely. – Alok Save Jun 01 '12 at 11:46
  • Well, I guess I'd be sitting here until an agreement is reached. :) – Some Newbie Jun 01 '12 at 21:34
  • @SomeNewbie, Understand in this way: (Q1) What's guaranteed effect of `inline`? (A1) Even if a function body is visible to all .cpp files (translation unit) from a common header, the linker doesn't complain, as only 1 body is created. (Q2) What's the requirement of a `template` function ? (A2) `template` function body must be visible to all `.cpp` files. So it must be declared in a common header, otherwise it gives linker error. .... Now you can see that `inline`'s effect and `template`s requirement meet at same place. Thus, you can conclude that `template` functions are `inline` by default. – iammilind Jun 02 '12 at 06:19