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;
}
1

4 Answers 4

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;
}
7
  • 1
    ...can be defined in a single compilation unit (cpp file + includes) multiple times - did you mean different compilation units?
    – anatolyg
    Oct 27, 2011 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! Oct 27, 2011 at 21:36
  • Excellent and highly understandable explanation, sir/ma'am. Many thanks!
    – lorenzoid
    Oct 27, 2011 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. Oct 27, 2011 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, 2017 at 21:55
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.

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
1
  • 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. Oct 27, 2011 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.

1
  • 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, 2011 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.