I have the following in bla.h:
#include <iostream>
static inline void bla() {
static int x;
std::cout << "bla @ " << (uintptr_t)bla << ": x @ " << &x << std::endl;
}
Then bla() is called from 2 separate translation units resulting in this output:
bla @ 94796100194693: x @ 0x56376fe10178
bla @ 94796100194897: x @ 0x56376fe10180
This is a bit surprising to me after reading inline specifier.
It says:
In an inline function,
- Function-local static objects in all function definitions are shared across all translation units (they all refer to the same object defined in one translation unit)
In that sentence it does NOT restrict this to "with external linkage (e.g. not declared static)" like it does in the paragraph before that for "It has the same address in every translation unit".
So I expected the output to show different addresses for bla
but identical addresses for x
.
Is this a bug in g++?
Note: This is specific to the changed meaning of inline in C++17.
As Igor Tandetnik commented the error is on cppreference.com as they left out an important part of the standard:
The actual text of the C++ standard has this note in [dcl.inline]/6: "A static local variable in an inline function with external linkage always refers to the same object."
Emphasis mine. – Igor Tandetnik Oct 20 at 4:12
So the observed behavior is according to the standard.