C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Declare as constinit, define as constexpr?

From: Brian Bi <bbi5291_at_[hidden]>
Date: Tue, 12 Dec 2023 15:17:27 -0500
On Tue, Dec 12, 2023 at 3:05 PM Thiago Macieira via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Tuesday, 12 December 2023 17:00:59 -03 Lénárd Szolnoki wrote:
> > > constexpr are implicitly inline, so these two don't actually work
> > > independently. At a minimum you must ensure that the former is
> > > visible before
> > > the latter. Or write extern constexpr.
> >
> > constexpr is not implicitly inline for namespace scope non-template
> > variables.
>
> Are you sure? This is an area that changed in C++14 or 17. I might be mis-
> remembering the details, but something did change to make something
> implicitly
> inline.
>

Const variables at namespace scope have always had implicit internal
storage duration (a.k.a. "implicitly static"). In C++11 and later, this
also includes `constexpr` variables because `constexpr` implies const. In
C++17, inline variables were added as an exception to this rule: so if you
don't declare it inline, then it's implicitly static, but if you do declare
it inline, then it's just inline. In C++20, variables declared in modules
are also an exception: http://eel.is/c++draft/basic.link#3.2.1

It's correct that you can avoid this either by having the non-defining
declaration earlier in the TU or by putting `extern` on the definition.


>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel DCAI Cloud Engineering
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>


-- 
*Brian Bi*

Received on 2023-12-12 20:17:42