tags:

views:

204

answers:

4

let's say i have 3 functions inside a class :

class Foo {
    inline void FooInline() { /* bla bla */ }
    static void fooStatic();
    void foo();
};

as i understand the last two have external linkage while the first have internal.

i want to know which function will be the fastest to call to , and what's the tradeoff.

thanks

+3  A: 

No, all three have external linkage. Member functions of a non-local class always have external linkage in C++. Moreover, inline has no effect on linkage, even if it is a non-member function.

Linkage has no effect on efficiency. Inlining might have, but it depends on too many variables.

AndreyT
Not true. inline implicitly makes the function static, and static gives the function internal linkage.
Tomas
@Tomas - the C++ standard specifically says "The inline keyword has no effect on the linkage of a function"
Michael Burr
@Michael Burr: from here: http://msdn.microsoft.com/en-us/library/z8y1yy88(VS.71).aspxFunctions that are declared as inline, and that are not class member functions, have internal linkage unless otherwise specified.Not sure if Microsoft specific.
BostonLogan
Generally, visual studio is not known as a shining beacon of standards compliance. Hopefully, they can redeem themselves with C++0x.
caspin
@Tomas: Absolultely not true. `inline` in C++ has no effect on function's linkage. And it certainly doesn't "make function static". Where did you get that strange information?
AndreyT
@BostonLogan: What MSDN says is complete nonsense. In C++ language `inline` has no effect on linkage. If MS compiler is consistent with MSDN, it is a serious bug in MS compiler. On top of that, note that in the OP we are talking about member functions and MSDN specifcially talks about non-member ones.
AndreyT
I just made a couple of tests with MSVC++ 2005 and it seems to handle inline functions correctly. I.e. the behavior is consistent with *external* linkage. Apparently, it is purely a MSDN's problem: what MSDN says is either total BS or already obsolete.
AndreyT
Yes you are right. I tried it too. Same function address consistent with external linkage.
BostonLogan
A: 

inline doesn't have "linkage" per say, it usually (though the compiler does not have to comply) just puts the code literally, uhh, inline.

You see internal linkage with anonymous namespaces and (standalone, not class) static functions, e.g.

namespace
{
  void foo() { ... }
}

Or:

static void foo() { ... }

Internal linkage means that the compiler can do some extra optimization (because it knows exactly how the function is being used) and doesn't have to create an exported symbol, which means less relocations on startup (meaning faster startups -- though modern linkers do lazy symbol resolution...)

scotchi
inline functions will never be exported; thus inline implies internal linkage.
Tomas
@Tomas: Incorrect. `inline` does not imply internal linkage.
AndreyT
@scotchi: Not true. All functions in C++ program have external linkage by default, Adding inline doesn't change anything with regard to linkage. Also, formally, a function declared in an anonymous namespace still has external linkage, even though you can't "name" it from other translation units.
AndreyT
A: 

The second one has internal linkage since you declared it static. It can be referenced only in the same translation unit.

Rob Pelletier
The OP explicitly states that the function is a member of a class. `static` has no effect on linkage of class members.
AndreyT
Good point -- read the original post too quickly and missed that. Alas!
Rob Pelletier
+1  A: 

just to be clear.

class fooClass
{
   inline void fooInline() { /*blah blah*/ }
   static void fooStatic() { /*blah blah*/ }
   void foo() { /*blah blah*/ }
};

As noted elsewhere. inline has no affect on linkage. Also static used as above in a method declaration/definition does not carry any linkage semantics. static is unfortunately an over used as a keyword. It effects linkage when used on global/namespace scope variables/functions. It has a totally different meaning when applied to class methods.

As for your question they all have external linkage. They all have the same performance when called. The inline MIGHT have and advantage IF the other two function are defined in a cpp file instead of the class AND the compiler decides it will be faster to inline calls to fooInline. fooInline will have no advantage in the source file where foo and fooStatic are defined.

All of this sounds like premature optimization. There are other problems worth tackling that will make your code much faster.

caspin