3

I have read about inline variables here and here but both links fail to mention whether an inline variables are inline in sense that they could be inlined by the compiler just like inline functions. that is replaced with the actual value on call site.

I know inline variables have external linkage and unique address, but are they inlined like functions? does that property apply to inline variables?

metablaster
  • 1,958
  • 12
  • 26
  • 6
    `inline` does not mean a function is to be inlined. It's more of a linkage specifier – StoryTeller - Unslander Monica Oct 11 '19 at 13:32
  • 1
    variables are already "inline" (like how a function can be). There is no call to get its value as the value is already known. This applies to all variables. – NathanOliver Oct 11 '19 at 13:34
  • @Story Teller yes, `inilne` function specifies external linkage, but also means optimization hint to the compiler. – metablaster Oct 11 '19 at 13:35
  • 1
    I was curious about why people still get inline wrong; I found this. http://www.cplusplus.com/articles/2LywvCM9/ I just died a little inside. – UKMonkey Oct 11 '19 at 13:37
  • A hint that implementations often ignore, but sure – StoryTeller - Unslander Monica Oct 11 '19 at 13:37
  • *but also means optimization hint to the compiler.* not really. Yes, it is a hint, but the compiler is free to, and normally will ignore it unless it's algorithms tell it it should inline the function. – NathanOliver Oct 11 '19 at 13:37
  • @metablaster: "*also means optimization hint to the compiler.*" Does it? Will a compiler *not* inline a non-`inline` function where it would inline an `inline` function? – Nicol Bolas Oct 11 '19 at 13:38
  • note that every compiler gives you an option (issue a warning) to warn you if a function marked `inline` whether explicitly or implicitly is not being inlined. so you can take an action such as `__forceiline` or give up from inlining. – metablaster Oct 11 '19 at 13:40
  • 2
    @metablaster I have never heard that (and it doesn’t really make much sense). Can you link to such an option for any compiler? – Konrad Rudolph Oct 11 '19 at 13:41
  • 1
    @KonradRudolph https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4710?view=vs-2019 – metablaster Oct 11 '19 at 13:42
  • @NicolBolas Apparently at least GCC does that under `-O1`, yes. It’s specifically what `-finline-functions` (which is included in `-O2`) generalises to non-`inline` functions. – Konrad Rudolph Oct 11 '19 at 13:44
  • 1
    @KonradRudolph GCC has [-Winline](https://www.cleancss.com/explain-command/gcc/4848) – NathanOliver Oct 11 '19 at 13:45
  • @NathanOliver Oh wow, I stand corrected. Is this warning at all useful? – Konrad Rudolph Oct 11 '19 at 13:47
  • @KonradRudolph Maybe? If you have a function you want to absolutely guarantee is inlined then it could be. Very low latency applications come to mind like high frequency trading. You don't want to spend any time calling functions if you can help it otherwise you might miss the market event. – NathanOliver Oct 11 '19 at 13:49
  • @KonradRudolph maybe it should be categorized as "information" instead of warning, I find it useful to decide what and when to inline . – metablaster Oct 11 '19 at 13:49
  • 1
    @NathanOliver I still don’t see this making sense, especially with templates, since ~95% of your functions will be declared `inline`, and most of them *shouldn’t* be inlined at every call site. So most of the output produced by this warning is bound to be noise (in fact, the last time I used MSVC, which was way more than a decade ago, warning level 4 was completely unusable due to false positives, but that might have been caused by other warnings). – Konrad Rudolph Oct 11 '19 at 13:50
  • _"replaced with the actual value on call site"_ How would you see this happening for a **variable** whose value is not known at compile time? (A `constexpr` would be a special case, not the general one.) – JaMiT Oct 11 '19 at 13:51
  • What does it even mean for a variable to be inlined? Or rather, what does it mean for a variable to *not* be inlined? What machine code do you imagine the compiler could generate, and how would that code differ between these two cases? – Igor Tandetnik Oct 11 '19 at 14:43
  • @IgorTandetnik My understanding: If the variable is inlined, the machine code might say something like *"put the value 42 in register A"*. If the variable is not inlined, the machine code might say something like *"put the value from memory address 0xFF02 in register A"*. The latter case can be slower since it involves accessing a memory address. – JaMiT Oct 11 '19 at 14:56

1 Answers1

2

both links fail to mention whether an inline variables are inline in sense that they could be inlined by the compiler just like inline functions.

Most likely that is because there is no reason to mention such a thing. You are basing your expectation on a false premise. The inline keyword has no bearing on whether or not a function can be inlined by the compiler. A compiler can choose to inline functions not marked inline, and it can choose to not inline functions marked inline. This has been the case from the beginning, even when the inline keyword was first introduced.

There was a time when the inline keyword was a hint to the compiler, but even then it was only a hint. The compiler was always free to make its own decisions about which functions to inline. Since that time, compilers have become better than programmers when it comes to deciding which functions are efficient to inline. So most modern compilers ignore the supposed hint (at full optimization).

that is replaced with the actual value on call site.

This cannot be done in general. A function call can be replaced by the function's content (its body) by the compiler because the compiler knows what the function's content is. In contrast, a variable's content (its value) is not known by the compiler. That is, after all, often the point of declaring a variable. Since the compiler does not know the "actual value", there is no way for the compiler to use that value as a replacement for fetching the value from the variable's location in memory.

This can be done, though, in the specific case where the compiler can deduce what the value of the variable will be. As with functions, it is up to the compiler to decide whether to use that value directly in the machine instructions or to retrieve that value from the variable's memory location. The use of keywords has no bearing on this decision (although at full optimizations, I would expect the value to be used as long as it fits in a register). The only keyword that is relevant is constexpr, which makes it much easier for the compiler to deduce what the value of the variable will be.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • Thanks for confirming, I realized my question does not make much sense after few useful comments in comment section. – metablaster Oct 11 '19 at 16:23