DEV Community

Discussion on: Unraveling the inline mess in C++

mdh81 profile image
mdh81 Author • Edited on

So what exactly would inline (or implicit inline within a class) do, then? And how would you request relaxing ODR if it's not inline?

In an ideal world, I'd expect ODR violations only when there are two or more distinct definitions for a function. In other words, if src.cpp includes a.h and b.h and they both have a definition for foo(), then there is a true problem. If I have src1.cpp and src2.cpp and they both include a.h, I wish there was an intelligent mechanism to determine that only a single definition of foo exists.

I realize this is easier said than done since the compiler only deals with a single compilation unit at a time. But, strictly speaking, there is no programmer error here, there is only one definition of the function, but because of the way inner machinery of the language works, the linker sees multiple definitions of the same function.

Since the burden is on the programmer to resolve this, I'd have preferred a less confusing resolution than overloading the use of inline keyword. I'd have preferred inline do it's original job and have a separate keyword for marking functions that have a single definition.

I'd think the warning should be opt-in rather than opt-out; otherwise it likely would get too noisy.


BTW: do you prefer the way inline is implemented in C instead?

Haven't programmed in C since my college days :) It's not clear to me how this is better than C++. Again, for me, overloading the inline keyword to control enforcement of one-definition rule seems complicated. I'd have preferred a separate keyword.