2

Possible Duplicate:
C/C++: Static function in header file, what does it mean?

I have found a function in code that I am working with:

in msgparser.hpp

static inline msgEnum msg2enum(char *str)
{
    msgEnum enumValue;

    ....

    return enumValue;
}

The function parses the string and determines which enum it matches with from a pre-defined array of message strings.

It is a free function.

I know what inline means - a hint to the compiler that the function should be inlined.

My questions are:

What does static mean for a free function in a header?

Can it be meaningfully combined with inline?

Community
  • 1
  • 1
DanS
  • 1,677
  • 20
  • 30
  • @sharptooth, perhaps a special case worthy of new answers since the `static` is not needed when the function is marked `inline`. – edA-qa mort-ora-y Jul 19 '11 at 12:19
  • Isn't there a notable difference between C and C++ here? I find no mention of free static functions in C++0x FDIS, only with regard to variables and member functions? – rubenvb Jul 19 '11 at 12:23
  • @rubenvb: They behave the same in C and C++ prior to C++0x. – sharptooth Jul 19 '11 at 12:27
  • 1
    To answer the part of this question that the "duplicate" doesn't: `inline` won't have its usual effect on a `static` function, since its purpose is to allow functions with *external linkage* to be defined in multiple compilation units; the `static` function has *internal linkage*, so can already be multiply defined. It may still act as a "hint" to inline function calls, if the compiler pays any attention to that. In a header, you're better off marking the function just `inline`, so that you should only end up with a single version of it. – Mike Seymour Jul 19 '11 at 12:28
  • 1
    If the user compiles with optimization off (`-O0` usually), then `inline` gets ignored and functions defined in headers get multiply instantiated (read: link fails), *unless* they are also marked `static`. This is not an unusual case: Sometimes, when hunting a difficult bug with a source-level debugger, you'll want inlining (and other optimizations) turned off to keep from breaking the single-step debug function. – Mike DeSimone Jul 19 '11 at 12:43
  • @Mike DeSimone: regardless of optimisation, you're allowed multiple (identical) definitions of `inline` functions with external linkage; they don't need to be (and usually shouldn't be) declared `static`. It's up to the linker to identify `inline` function definitions, and discard duplicates without error. – Mike Seymour Jul 19 '11 at 13:40
  • OK, the linker wasn't that smart back when this last bit me in '02. – Mike DeSimone Jul 19 '11 at 16:17

0 Answers0