DEV Community

Discussion on: Unraveling the inline mess in C++

Collapse
 
pauljlucas profile image
Paul J. Lucas

In short, I'm not a fan of this C++ feature.

So what would you have the compiler do instead?

Collapse
 
mdh81 profile image
mdh81

1) Not overload inline keyword for ODR purposes
2) Emit a helpful message when an inline request cannot be fulfilled so developers understand compiler’s rationale.

Collapse
 
pauljlucas profile image
Paul J. Lucas

1) Not overload inline keyword for ODR purposes

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?

2)

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?

Thread Thread
 
mdh81 profile image
mdh81 • Edited

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.

Agreed.

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.