8 ms·
Summary of the semantics of inline in C and C++
- flohofwoe 5y agoMy (probably unpopular) opinion is that 'inline' should never have been added to the C standard, because it breaks the strict separation of interface declaration (in headers) and implementation code (in source files). Inline in C++ is the source of all sorts of problems, the biggest one being slow compilation because the same inline code in headers needs to be parsed over and over again. Within the same compilation unit, the compiler will inline any suitable function anyway, and 'static' alone is enough to hint the compiler that there's no separate copy of the function needed if it can be inlined at all call sites. And for inlining across compilation units, there's LTO these days (which admittedly wasn't a realistic option in the late 90's).
- InitialLastName 5y agoIf you're putting an inline function into your header file, the size of the code for that function is part of the information provided by the interface (i.e. the inline code is getting stored with the user). Every user has to know they're duplicating that code locally. I agree, though, that for the most part there are probably better ways to do it (including trusting in "the compiler is a better optimizer than my wetware").
- zabzonk 5y agoFor C++ (C and C++ differ a lot here): https://en.cppreference.com/w/cpp/language/inline https://en.cppreference.com/w/cpp/language/inline The most common use of inline in C++ is to define a function in a header file that is potentially #included in multiple source files. Without the inline specifier, this would result in multiple definition errors at link time. It's an example (there are several in C++) of reusing syntax/semantics to replace something no-one does (trying to get the compiler to inline code) with something that is useful.
- ridiculous_fish 5y agoRight, the inline keyword in C++ is mainly used to dodge ODR. When you really care about inlining, you want an attribute like always_inline, which emits an error if the function cannot be inlined (e.g. is recursive).
- varajelle 5y agoLittle to do with ODR. The inline function must have the same tokens in every translation unit otherwise it is undefined behaviour.
- dataflow 5y agoSame tokens? So you're saying 'inline int foo(void);' + 'typedef int I; inline I foo(void);' is UB?
- MauranKilom 5y agoQuoting the C++ 17 standard draft [0]: > There can be more than one definition in a program of [...an] inline function [...] as long as all of the following is true: > - each definition consists of the same sequence of tokens (typically, appears in the same header file) > [...] > If all these requirements are satisfied, the program behaves as if there is only one definition in the entire program. Otherwise, the program is ill-formed, no diagnostic required. [0]: https://timsong-cpp.github.io/cppwp/n4659/basic.def.odr#6.1 https://timsong-cpp.github.io/cppwp/n4659/basic.def.odr#6.1
- xxpor 5y agoNo one tries to get the compiler to inline code? What? Doing a function call is quite expensive! If you're not worried about that level of performance why are you using C these days in the first place?
- jeffbee 5y agoThe only legitimate way to influence the compiler's inlining decisions is by profile guidance. If you aren't providing a profile at compile time, you demonstrably do not care about performance.
- br1 5y agoIt would be ironic if Rust ended up killing C and not C++, being adopted into the kernel.
- tyingq 5y agoIf I look at a list of Rust projects (awesome-rust, etc), quite a lot of them either replicate something done in C, or create something that probably would have been done in C.
- pjmlp 5y agoTo kill C++, Rust needs to grown an ecosystem for game engines, GPGPU shading languages (Metal/HLSL/CUDA), OS GUI stacks (Qt/WinUI) and composition engines,... It might happen, in a couple of decades though.
- humanrebar 5y agoAlso, nothing is even close to matching the ability of C++ to build against other C++. Even if Rust had Rust alternatives for enough of the interesting niches, it will take a loooong time for all the relevant software to either get rewritten or reach end of life.
- pjmlp 5y agoYes, a good example is that despite 40 decades of trying, there are still domains that C++ failed to take away from C. Companies rather invest into hardware memory tagging than trying to rewrite C applications into something else, in those domains.
- Hello71 5y agoas pointed out in the reply, this is incomplete. there are actually three versions of inline in common use in C-like languages: gnu89, c99, and c++, which are all different.
- klodolph 5y agoHere is sample code that illustrates how it works. The approach to using inline functions in C is a bit counterintuitive. Here is how you might define a normal function in C: // file.h int add_one(int x); // file.c #include "file.h" int add_one(int x) { return x + 1; } Here is how you might define it inline: // file.h inline int add_one(int x) { return x + 1; } // file.c #include "file.h" int add_one(int x); Seems backwards to have a definition in the header and prototype in the implementation file, but because the prototype is "extern" (which is default, and can be omitted), and because the definition + declaration merge in certain ways (extern + inline = extern inline), you end up with an "extern inline" definition which means "emit the code in this translation unit, please." If LLVM's behavior is counterintuitive, it's probably because it's there to support counterintuitive parts of the C language. This frees you from making decisions in a sense, because the compiler can decide whether to use the inline or extern definition at any given call site. Again, the syntax for it is counterintuitive. The other way is: // file.h static inline int add_one(int x) { return x + 1; } This may give you multiple copies of the code (emitted in potentially any translation unit it appears in), but it's less typing. Omitting any discussion about C extensions here.
- ridiculous_fish 5y agoAny guesses as to why C chose such crazy semantics for inline functions? "The compiler can decide to use the inline or extern definition" is begging for trouble. "inline functions cannot have their address taken and cannot have static variables" seems so natural and obvious.
- klodolph 5y ago> "The compiler can decide to use the inline or extern definition" is begging for trouble. IMO this behavior seems like the obvious choice, but I've been writing C long enough that I'm sure my perspective is distorted and my sense of what is obvious is completely out of whack. C++ made the choice that linkers should be able to coalesce duplicates, C made the choice that linkers do not need this feature. If you want to inline, you want definitions in every translation unit. If you don't want to have to inline, you want to pick a translation unit which gets the code. The compiler is what chooses whether a function is inline, and programmers should not think of the "inline" keyword as affecting that choice. > "inline functions cannot have their address taken and cannot have static variables" seems so natural and obvious. Yes, obvious that they should not have static variables. There's nothing wrong with taking the address of an inline function, though. It behaves just like a normal function. Pointers to a function will compare equal if and only if they point to the same function--a function with external linkage is the same function in every translation unit, a function with internal linkage is a different function in every translation unit. Whether or not the function is inline does not matter (why would it?) This means that you can take functions in an existing C code base, and you can make them inline, without any change in functionality. This should always work, barring... you know... those weird unexpected consequences. Isn't that nice though? Whether a function is inline is a detail that you don't have to care about, unless you're defining the function. Remember that "inline" does not mean, "this function will be inlined". Instead it means, "the compiler may choose to inline this, or not, at its discretion." Which really just means, "the definition for this function is visible in the translation unit where it is called", because the compiler can generally choose to inline any function if the definition is there. So, the keyword could be renamed to reflect what it means. Instead of: // file.h inline int add_one(int x) { return x + 1; } // file.c int add_one(int x); You could choose a different name for the keyword: // file.h do_not_emit_code_in_this_translation_unit_by_default int add_one(int x) { return x + 1; } // file.c override_default_and_omit_code_in_this_translation_unit int add_one(int x); I suspect "inline" was considered easier to type.
- the_only_law 5y agoThese are the sort of reasons I’ll never be able to use C or C++ in any real capacity. Sure I can read it and to a much lesser write it, but there’s way to many crazy things dependent on the compiler/platform that I never run into and only ever see in passing contexts like this. I think I’d trust my self to learn Haskell before I ever trust myself to write production C.
- rramadass 5y ago>I think I’d trust my self to learn Haskell before I ever trust myself to write production C. Hyperbole much? This is just one of "those" things which you only learn when you need it. In actual usage, myself and most people i know have never used "inline" in C. Macros are the time honoured way of doing such things. There is no mystery to it but just a choice and convention (compared to C++).
- the_only_law 5y ago> Hyperbole much? Nope, I fully believe this. > This is just one of "those" things which you only learn when you need it. And how will you know when you need it when you don’t know it?
- pjmlp 5y agoAfter spending hours trying to figure out why something doesn't work and by chance landing on the above post when searching for inline and C.
- rramadass 5y agoYour argument really does not have a leg to stand on. The concept of "inlining" a function is Programming 101. The mechanism of how to achieve it is what is specific to a language/runtime. You check the syntax/semantics/documentation and figure it out aka problem-solving. Not a big deal and no need to make unwarranted claims/charges against the language as a whole.
- 5y ago