4 ms·
It’s more precise to say that inline means “this definition might be duplicated.” Non-inline functions also are allowed to have their definitions provided inlin
by tylerhou 2y ago
It’s more precise to say that inline means “this definition might be duplicated.” Non-inline functions also are allowed to have their definitions provided inline with the declaration; the difference is that the linker will error if it encounters a duplicate non-inline definition.
- josemanuel 2y agoAre you thinking of "weak" rather than "inline"? I always thought of "inline" as a hint to the compiler -- the compiler can inline, but it may not, depending on some compiler-specific optimisation objective (execution speed, code size, ...).
- mgaunard 2y agoYou thought wrong. Marking the symbol as weak is what inline indeed does from a technical point of view.
- tylerhou 2y agoNo, I am not thinking of weak; this is why “inline does not mean inline” is on the iceberg :). Although practically speaking inline is also used as a hint. But chiefly it refers to inline linkage. I’m actually also not familiar with weak, but a quick glance suggests that it might not mean the same thing as inline. Can weak symbols be defined multiple times? EDIT: yes, according to an SO post compilers often use weak to implement inline.
- mgaunard 2y agoI fail to see how it's more precise. Obviously if the definition is inline, it's duplicated in every point where the declaration is imported. Beginners fail to understand for example that providing a class member function definition within the class definition makes it inline, but that's already what you're doing textually, and that's what the keyword means.
- wolletd 2y agoYou can declare and inline-define a function in a CPP file and you don't need inline because there are no other places that can import it. It's only needed for functions defined in header files where multiple definitions may exist after compiling object files.
- gpderetta 2y agoIf you textually define a function "inline" in an header file yet do not mark it inline, you are duplicating it in every point where the declaration is imported, yet, not being inline for linkage purposes, you will get compilation errors. Member and template functions are special cased to be implicitly inline.
- mgaunard 2y agoThat's not an inline definition, that's a normal definition, which should never be in a header file. Non-inline definitions need to be in their own translation unit.
- gpderetta 2y agoOf course, but that disproves that an "inline definition is a definition that is provided inline with a declaration" [in an header file]. An inline definition is a definition that is marked inline, implicitly or explicitly. Whether it is defined together with the declaration or in an header vs translation unit is immaterial.