3

What's the difference between inline function and then main like so:

inline double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

vs just declaring a function regularly like:

double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

vs function prototype?

double cube(double);

int main( )
{
    cube(5);
}

double cube(double side)
{
   return side * side * side;
}
lorenzoid
  • 1,812
  • 10
  • 33
  • 51
  • possible duplicate of [c++ inline function?](http://stackoverflow.com/questions/5971736/c-inline-function) -- also cf. [this answer of mine](http://stackoverflow.com/questions/7738888/what-is-concept-of-inline-function-and-how-it-is-differ-from-macro/7739024#7739024). – Kerrek SB Oct 27 '11 at 23:37

4 Answers4

6

An inline function can be defined in multiple translation units (cpp file + includes), and is a hint to the compiler to inline the function. It is usually placed in a header which increases compile time, but can lead to faster code. It also allows the function to be used from many compilation units.

//cube.h
inline double cube(double side)
{
   return side * side * side;
}

//cube.cpp
int main( )
{
    cube(5);
}

Defining it regularly is the normal method, where it's (usually) defined in a cpp file, and linked against. It is not easily used from other compilation units.

//cube.cpp
double cube(double side)
{
   return side * side * side;
}

int main( )
{
    cube(5);
}

A prototype allows you to tell the compiler that a function will exist at link time, even if it doesn't exist quite yet. This allows main to call the function, even though it doesn't exist yet. Commonly, prototypes are in headers, so other compilation units can call the function, without defining it themselves. This has the fastest compilation time, and the function is easily used from other compilation units.

//cube.h
double cube(double);

//cube.cpp
int main( )
{
    cube(5);
}

double cube(double side)
{
   return side * side * side;
}
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • 1
    *...can be defined in a single compilation unit (cpp file + includes) multiple times* - did you mean __different__ compilation units? – anatolyg Oct 27 '11 at 21:25
  • Nope, `inline` is allowed to violate the ODR, like templates. Otherwise, it'd be hard to put an inline in the header, and if it weren't in the header, stupid compilers couldn't inline it! – Mooing Duck Oct 27 '11 at 21:36
  • Excellent and highly understandable explanation, sir/ma'am. Many thanks! – lorenzoid Oct 27 '11 at 21:37
  • 1
    @MooingDuck: anatolyg is right, an `inline` function can be defined *only once* in a translation unit, even if it can be defined multiple times in a program without violating the ODR. – David Rodríguez - dribeas Oct 27 '11 at 22:14
  • The last example is undefined behaviour with no diagnostic required , if `cube.h` is included by both `cube.cpp` and another translation unit. A function may not be `inline` in one unit and non-inline in another. – M.M Nov 19 '17 at 21:55
  • In the second example it would be good practice to mark the function as internal linkage to avoid accidental undefined behaviour in case some other part of the project also makes a function called `cube` – M.M Nov 19 '17 at 21:56
  • @M.M: Fixed the extra `inline` in the last sample, thanks. You're correct about internal linkage, but that's extra complexity that isn't super relevent to the answer. – Mooing Duck Nov 20 '17 at 19:27
1

Performance wise, they are all the same since inline is just a hint for the compiler. If the separation of declaration/definition is used and the definition is on a different translation unit, then it will be harder for the compiler to inline it (but there are implementations that do so).

A difference with making a function inline or not is that the linker will not complain if it sees the same inline definition for a function more than once.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

The 3 program compile to exactly the same with g++ -S -O3 $file.cc. Except for the second example where the definition of double cube(double side) still exist in a non inlined form though inlined in int main().

_main:
pushl   %ebp
movl    $16, %eax
movl    %esp, %ebp
subl    $8, %esp
andl    $-16, %esp
call    __alloca
call    ___main
leave
xorl    %eax, %eax
ret
log0
  • 10,489
  • 4
  • 28
  • 62
  • The difference that you point out is a detail of implementation, some compilers will generate all functions always even if it is actually inlined because you are allowed to obtain the address of an `inline` function, and the compiler might not know while processing the function if later in the same TU that function's address will be requested. – David Rodríguez - dribeas Oct 27 '11 at 22:18
0

When you declare a function inline the compiler tries to speed up the code by more or less copying the body of the function to where it's called. It's only a suggestion and it's up the the compiler to make the decision on if it's possible.

I'm not sure what would happen in the 3rd example. I imagine it would depend on the tool chain being used.

offtehcuff
  • 128
  • 5
  • The last one is just a function prototype before the main(). They all seem to work the same way, but I don't seem to understand what the difference is. – lorenzoid Oct 27 '11 at 21:13