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 <cpp....@gmail.com> 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 <cpp....@gmail.com> 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

Reply via email to