views:

184

answers:

3

Is the following free function implicitly inlined in C++, similar to how member functions are implicitly inlined if defined in the class definition?

void func() { ... }

Do template functions behave the same way?

+1  A: 

It might be inlined, depending on if the compiler decides to make it inline or not.

Magnus Skog
+6  A: 

It depends what you mean by inlined. A compiler can optimise any function by placing its emitted code inline at the call site. However, if you mean does the code you ask about behave as if it was declared:

inline void func() { ... }

then the answer is no. If you place your code in two different compilation units and build the executable, you will get multiple definition errors. If you explicitly mark the function as "inline", you will not.

Regarding template functions, then some part of the compilation system will see to it that multiple instantiations of the same template do not cause multiple definition errors.

anon
Would it be correct to assume that "inline void func() {...}" inside a .cpp file is the same as "static void func() {...}", assuming the appropriate inlining options are enabled for the compiler?
Mr Fooz
It appears to be possible (at least with g++) to declare inline functions as extern, but not possible for statics. I leave it to someone else to do a Standard trawl on this - it's too hot here in London.
anon
It is absolutely possible to have an extern inline function. Indeed, the standard explicitly mentions it: "A static local variable in an extern inline function alwaysrefers to the same object. A string literal in an extern inline function is the same object in different translation units."
Doug
It's perfectly possible to have a static inline function (there's an example in 7.1.1[dcl.stc]/7) but the linkage implied by all declarations of a function must agree. There's strong coupling between this section and 3.5[basic.link].
Charles Bailey
An `inline void func() {...}` is a extern function. Its static variables in all definitions of it in the program are the same, and the function address too, because in all cases, the compiler link all definitions to the same function. It's thus not the same as `static void func() {...}` where each definition defines a separate distinct function.
Johannes Schaub - litb
+4  A: 

No, it's not implicitly inlined. The compiler has no way of knowing if another module will use this function, so it has to generate code for it.

This means, for instance, that if you define the function like that in a header and include the header twice, you will get linker errors about multiple definitions. Explicit inline fixes that.

Of course, the compiler may still inline the function if it thinks that will be efficient, but it's not the same as an explicit inlining.

Template functions are implicitly inlined in the sense that they don't require an inline to prevent multiple definition errors. I don't think the compiler is forced to inline those either, but I'm not sure.

CAdaker