Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Definition Rule - Multiple definition of inline functions

Tags:

c++

c++11

I was reading ODR and as the rule says "In the entire program, an object or non-inline function cannot have more than one definition" and I tried the following...

file1.cpp

#include <iostream>
using namespace std;

inline int func1(void){ return 5; }
inline int func2(void){ return 6; }
inline int func3(void){ return 7; }
int sum(void);

int main(int argc, char *argv[])
{
    cout << func1() << endl;
    cout << func2() << endl;
    cout << func3() << endl;
    cout << sum() << endl;
    return 0;
}

file2.cpp

inline int func1(void) { return 5; }
inline int func2(void) { return 6; }
inline int func3(void) { return 7; }
int sum(void) { return func1() + func2() + func3(); }

It worked as the rule says. I can have multiple definition of inline functions.

  • What is the difference between non-inline function linkage and inline function linkage?
  • How the linker differentiate between these two?
like image 546
NJMR Avatar asked Sep 23 '16 04:09

NJMR


People also ask

How do you define an inline function?

Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.

What is ODR used?

In plain word, odr-used means something(variable or function) is used in a context where the definition of it must be present.

What are the restriction of inline function?

Limitations of Inline FunctionsInline functions do not work if the body of the function contains any sort of looping or iteration. Inline functions do not support the use of switch or goto statements. C++ Inline functions cannot work if the function defined is recursive in nature.

What is the difference between inline function and?

Difference Between Inline and Normal Function in C++ Inline Function is a function that is expanded inline by the compiler when it is invoked. During function call, a lot of overhead tasks are performed like saving registers, pushing arguments to the stack, and returning to the calling function.


1 Answers

Making a function inline does two things (the second point is more relevant to your question):

  1. It is a suggestion by the programmer to the compiler, to make calls to this function fast, possibly by doing inline expansion. Roughly, inline expansion is similar to treating the inline function like a macro, expanding each call to it, by the code of its body. This is a suggestion - the compiler may not (and sometimes cannot) perform various optimizations like that.

  2. It specifies the scope of the function to be that of a translation unit. So, if an inline function appears in foo.cpp (either because it was written in it, or because it #includes a header in which it was written, in which case the preprocessor basically makes it so). Now you compile foo.cpp, and possibly also some other bar.cpp which also contains an inline function with the same signature (possibly the exact same one; probably due to both #includeing the same header). When the linker links the two object files, it will not be considered a violation of the ODR, as the inline directive made each copy of the file local to its translation unit (the object file created by compiling it, effectively). This is not a suggestion, it is binding.

It is not coincidental that these two things go together. The most common case is for an inline function to appear in a header #included by several source files, probably because the programmer wanted to request fast inline expansion. This requires the translation-unit locality rule, though, so that linker errors shouldn't arise.

like image 172
Ami Tavory Avatar answered Oct 04 '22 07:10

Ami Tavory