Re: "static inline" in a header file is stupid, right?
Hi, I think your original message was meant to be about `static inline` function definitions in a header file. To my understanding, nothing has changed with C++11/14/17 about that. However, I think it wasn't accurate before that, since there is a difference between `static inline` member functions and `static inline` free functions. The former make sense, and in case of templates may even be required to be in a header file. However, I think that this is mostly unrelated to the use of the `inline` keyword here, but applies to any `static` free function declared in a header file: If it is inline (explicitly or implicitly), it would emit one copy per compilation unit, and if it were not inline, it wouldn't be callable outside the compilation unit that contains its definition. Note however, that whether a function is `inline` in the sense of the C++ standard is not related to whether the compiler (or, with LTO, the linker) will inline it during code generation. But there are now `static inline` member variables as you point out, which also make sense to be used in a header file. The simple grep as suggested in the original message does not distinguish these cases. (OTOH, the order of the keywords could be reversed, since `static inline` and `inline static` are equivalent, and the grep as such does not match the latter.) We already have several static member variables defined in header files, which are implicitly inline because they are `constexpr`, most of them within media/webrtc/trunk/webrtc as of now. Simon On Mon, Aug 3, 2020 at 2:05 PM wrote: > 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 > ___ 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
Re: "static inline" in a header file is stupid, right?
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 ___ 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?
> > Hi! I come from the future, it is not in C++17. > Can you elaborate on what "it" is? Thanks, Botond ___ 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 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 Tuesday, April 3, 2012 at 10:41:37 PM UTC+2, Kyle Huey wrote: > That's true for things at file scope, yes. My not-very-random sample of > MXR indicates that this is often used on static things on structs/classes, > which has an entirely different meaning of course. > > - Kyle Static Inline makes perfectly sense in C++ Consider the following use case: (in principle I think its irrelevant if the function is static or not) You have a library/DLL for which you in some case wants to link in the library whereas for other occasions you just want to use a small set of functions, and for this you use the inlined versions to avoid the hassle of having to ship also the DLL. The decision on whether to use the inlined version or the the DLL (the code should be the same) is taken by the compiler. If the "inline" keyword is not used then the implementation will be taken form the DLL wg\hen generating the object files, even if the implementation is present in the header file. Now, when linking, you will get an error of the form "XYZ allready defined in." Because the function is implemented multiple places, firstly, in the DLL, the in every place where the header file containing the function is included. Using "inline" will only be necessary in front of the implementation, however in my opinion it is a good idea to add inline also in form of the function definition in order to show intent, even if the keyword has no effect. ___ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform