0

As explained in this document:

Is there still a use for inline?

So, what is the actual purpose of the inline keyword? It's simple: a function marked inline can be defined in more than one translation unit without violating the One Definition Rule (ODR). Imagine these two files:

but using inline in this way has a problem as explained in this document:

https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rzarg/inline_linkage.htm

Redefining an inline function with the same name but with a different function body is illegal; however, the compiler does not flag this as an error, but simply generates a function body for the version defined in the first file entered on the compilation command line, and discards the others.

Is there a way that I can define a function in the header file and use it in multiple translations and if I accidentally redefine a function twice with two different function body, the compiler can generate an error? (assuming that all functions are inside the same namespace)

Is there any plan to have such keyword in further versions of C++

Edit

I already saw the answer that set as duplicate, but that question did not refer to issue that IBM reported on using inline functions. The duplicate explained that now a day, inline is used for preventing multi-translation, which I also referred in my question, but there is no reference to the problem that it generates.

Community
  • 1
  • 1
mans
  • 17,104
  • 45
  • 172
  • 321
  • Breaking ODR rules make program ill formed NDR. NDR as it is complicated to diagnose the violation in general case. – Jarod42 Jan 25 '19 at 16:09
  • @Jarod42 I know and my question is "What is the best way to make sure that Compiler can detect ODR and report it?" – mans Jan 25 '19 at 17:47
  • You can even have ODR violation without inline and header, if you define 2 different methods with same name in 2 translation units. local functions should be marked static, or in unnamed namespace to reduce risk of collision. – Jarod42 Jan 25 '19 at 17:53

0 Answers0