0

I know that the compiler can decide whether to inline a function or not. Lets say someone defined a function as inline in a header file and the function body is also in the header file. Also, say the function is long and the compiler decides NOT to actually inline this function.

f1.h:

inline void f1()
{
    /* really long function */
}

What now happens to the function when it is included in multiple cpp translation units -- would that not break the one definition rule and cause multiple definitions at linking time? Or does that inline property remain, even if it's not actually inlined?

foo.cpp:

#include "f1.h"
/* code here */

bar.cpp:

#include "f1.h"
/* code here */
simplename
  • 717
  • 7
  • 15
  • 2
    Did you research what `inline` means? See [cppreference inline specified](https://en.cppreference.com/w/cpp/language/inline) the `2.` point in Explanation. – KamilCuk Nov 11 '20 at 23:36
  • 4
    It doesn't matter if compiler inlines function or not. Keyword `inline` is your promise to the compiler, which says "There may be multiple definitions of this function, but I guarantee they will all be identical, allow it under ODR". – Yksisarvinen Nov 11 '20 at 23:36
  • Somewhat related: [Why are C++ inline functions in the header?](https://stackoverflow.com/questions/5057021/why-are-c-inline-functions-in-the-header) – Yksisarvinen Nov 11 '20 at 23:43

1 Answers1

2

In short: Any "optimization decisions" made by the compiler during compilation will never lead to a compile error. So the answer is NO.

Sven Nilsson
  • 1,861
  • 10
  • 11