Re: "static inline" in a header file is stupid, right?
On Tuesday, April 3, 2012 at 3:38:08 PM UTC-5, Benoit Jacob wrote: > Hello, > > Short version: "inline" alone is enough to take care of multiple > function definitions. Next time you're about to write "static inline" > in a header file, seriously consider doing "inline" instead. > > Long version: > > This command, > > $ find mozilla-central -name '*.h' | xargs grep -n 'static inline' > > finds 1851 matches here. > > The "inline" keyword does two things that I know of: > 1) it suggests inlining, and > 2) it allows multiple definitions of a function, regardless of > whether inlining happens. > > That second thing means that in most cases, defining a "static inline" > function in a header is useless, and just "inline" is what is really > wanted in most cases. > > The "static" keyword in "static inline" is harmful in the situation > where a "static inline" function gets included and compiled in N > different files and does not get inlined. In this situation, one would > typically want the N copies of this function to get merged into one, > to avoid code bloat. But the "static" keyword prevents that, forcing > the linker to not merge these redundant functions. > > Cheers, > Benoit Hi! I come from the future, it is not in C++17. ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: "static inline" in a header file is stupid, right?
On Friday, July 31, 2020 at 12:54:59 PM UTC-5, Botond Ballo wrote: > > > > Hi! I come from the future, it is not in C++17. > > > > Can you elaborate on what "it" is? > > Thanks, > Botond Stupid: It is not stupid, it is a feature. ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform
Re: "static inline" in a header file is stupid, right?
On Friday, July 31, 2020 at 8:21:01 PM UTC-5, Botond Ballo wrote: > On Fri, Jul 31, 2020 at 9:00 PM wrote: > > > Stupid: It is not stupid, it is a feature. > > > > Ah, you mean `static inline` has uses in C++17 that it didn't have in older > versions? > > I would appreciate an example (or a link to post etc.) so we can understand > this better. > > Thanks, > Botond Sure, before 17 you had to initialize static variable in cpp "encapsulated" code. So maybe you want to expose static data, specifically the way you initialized this static data, through your headers cause you want your API users to have access to read them and figure out things. Now, you can do that in your header file outside the class brackets. But if you included the header, in more than one user cpp file, the linker will complain with duplicated declaration of your static objects. Now with inline static, you can initialize in the class declaration. Whenever may be the case you want your API users (if an API / Lirbary is what you are writing) to understand how's your static var is initialized. A link to show what I mean: https://www.tutorialspoint.com/how-do-inline-variables-work-in-cplusplus-cplusplus17#:~:text=The%20static%20value%20is%3A%2010,the%20class%20using%20inline%20variables. ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform