C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Complete-class contexts for inline static data member initializers

From: Cell <dicot0721_at_[hidden]>
Date: Fri, 20 Nov 2026 15:14:47 +0800
On Fri, Nov 20, 2026, 1:16 PM Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
wrote:

> On Thu, Nov 19, 2026 at 1:35 PM Cell <dicot0721_at_[hidden]> wrote:
>
>> On Thu, Nov 19, 2026, 11:07 PM Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
>> wrote:
>>
>>> On Thu, Nov 19, 2026 at 4:20 AM Cell via Std-Proposals <
>>> std-proposals_at_[hidden]> wrote:
>>>
>>>> Hello,
>>>>
>>>> I would like to discuss whether in-class initializers of inline static
>>>> data members should be considered complete-class contexts.
>>>>
>>>> [...]
>>>>
>>>> Are there fundamental semantic or implementation concerns with such an
>>>> extension?
>>>>
>>>> I would appreciate any thoughts or references to previous discussions.
>>>>
>>> Recently on my blog:
>>> https://quuxplusone.github.io/blog/2026/08/25/size-idioms/
>>>
>>> I have no special knowledge here, but it seems to me that you might run
>>> into confusing logic with things like
>>> struct X {
>>> static constexpr int f(unsigned) { return 1; }
>>> static constexpr int m_foo = X::f(0); // Ill-formed today. On
>>> first scan-through, by this point only f(unsigned) is visible; if we wait
>>> until X is complete, we'll call f(int) instead; surely we want the latter,
>>> right?
>>> static constexpr int f(int) { return 2; }
>>> };
>>> And, more to the point, circular logic with things like
>>> struct X {
>>> static constexpr int m_size = 42;
>>> int m_data[m_size]; // Well-formed today. If we wait until X is
>>> complete to figure out m_size's value, then wouldn't this become ill-formed?
>>> static constexpr int m_bar = sizeof(X); // Ill-formed today;
>>> but if it were well-formed, it would certainly need to know sizeof(m_data)
>>> already
>>> };
>>> Namely
>>> struct X {
>>> static constexpr int m_size = sizeof(X);
>>> char m_data[m_size + 1]; // What happens here??
>>> };
>>>
>>
>> Thanks, you hit the nail on the head! I hadn't even thought of that...
>>
>> But what if we limit our discussion to class templates? According to [
>> temp.inst <https://eel.is/c%2B%2Bdraft/temp.inst#4>], static data
>> members are not required to be initialized even when a class template is
>> implicitly instantiated. Does this mean that when static data members are
>> used in the complete class context, their initializers are also included in
>> that context? (Demo <https://godbolt.org/z/WznvfG8eW>.) That way, we
>> might be able to avoid the issue you mentioned.
>>
>
> I don't see how making X a template is going to solve anything.
> template<class T>
> struct X {
> static constexpr int m_size = sizeof(X);
> char m_data[m_size + 1]; // What happens here??
> };
> X<int> x; // Here it's instantiated
>
> Your demo example makes `m_size` non-constexpr, so that I can't use it as
> an array bound. So you could say, "What if we limit our discussion to the
> initializers of *non-constant* inline static data members?" In that case,
> I haven't got a good counterexample off the top of my head. I think it
> might indeed be possible to delay the initializers of non-constant inline
> static data members until we have a complete-class context to evaluate them
> in. But then I would argue that that's a *very* niche case — static,
> inline, non-const, with an in-class initializer, and that initializer
> depends on the complete-class context (or else it'd already be legal today)
> — and I just don't think that change, while *possible*, would actually
> end up *helping* anyone.
>
> my $.02,
> –Arthur
>

Thanks!

What I mean is: when a static data member is used within the complete class
context, the member's initializer is also within that context; *conversely*,
when the member is used outside that context, the initializer is also
outside that context.

In the example you provided, `m_size` is used in the declaration of a
non-static data member, but the declaration of a non-static data member is
not within complete class context (obviously, replacing `m_size` with
`sizeof(X)` would also fail to compile); this should be IFNDR.

According to [temp.inst <https://eel.is/c%2B%2Bdraft/temp.inst#3>], when
implicitly instantiating a class template, only the declaration of *"the
non-deleted class member functions, member classes, scoped member
enumerations, static data members, member templates, and friends"* and the
definition of *"deleted member functions, unscoped member enumerations, and
member anonymous unions"* are implicitly instantiated. In other words, as
long as `m_size` is not used in any of the locations mentioned above that
lie outside the complete class context, the definition of `m_size` should
be valid.

>

Received on 2026-11-20 07:15:04