C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Make inline implicit for static data members with in-class initialiser

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 18 Jan 2024 14:30:25 -0800
On Thursday, 18 January 2024 09:06:23 PST Tom Honermann via Std-Proposals
wrote:
> > In C++17, `constexpr` static data members were made implicitly inline
> > and the out-of-class definition was changed to be a redundant
> > redeclaration. In principle, this could be done for other types of
> > static data members too, couldn't it?
>
> I'm not sure. The kind of symbol produced could change and thereby
> impact compatibility with previously built code.

I don't think so. I think it can be done compatibly.

The inline in-class declarations produce a vague, weak symbol, but the out-of-
class definition produces a non-weak, non-vague entity. At link time, the
linker discards the vague symbols and keeps the non-vague. At dynamic link
time, the runtime linker does the same.

This is exactly what happened to the constexpr symbols. See
https://godbolt.org/z/b87xerEbT
Note how the C++14 declared the Strong::X variable in .rodata and without
.weak and not @gnu_unique_object. I had expected the C++17 version to do the
same if the definition was there too, but it emits the same code so long as the
variable is ODR-used.

What's more, this is pretty much the same as C common variables, except
they're initialised. Though C23 has moved away from common.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2024-01-18 22:30:27