C++ Logo

std-discussion

Advanced search

Re: How to force initialization of templated static variable in instantiated template?

From: Marios Staikopoulos <marios_at_[hidden]>
Date: Thu, 21 Jul 2022 22:25:42 +0000
This is an interesting approach, I think my modification with the empty local and yours do end up with the same result, but the interface in this case was part of the intent as well - I am experimenting with your approach to see if it could work (assuming the DummyHelper was changed to be templated on the type itself), but it also requires that DummyHelper in this case be defined *before* the definitions of the structs.

The intent itself was more so that there would be an explicit instantiation of `StaticInitializer<T>` for every class T that is introduced, such that in its invocation, any `runtime_state` data required could be initialized.

The dispatcher, initializers, etc can also be batched together in a generated include by a preprocessor.

Why this exact behavior is desired may be specific to my use case, which I can explain a bit here -
I have a preprocessor that generates templated constexpr reflection data, which is fine and extremely useful, but it doesn't help for the case when you are reading serialized entities abstractly from the disk and trying to re-enter into templated code 😊

-----Original Message-----
From: Std-Discussion <std-discussion-bounces_at_lists.isocpp.org> On Behalf Of Lénárd Szolnoki via Std-Discussion
Sent: Thursday, July 21, 2022 1:57 PM
To: std-discussion_at_lists.isocpp.org
Cc: Lénárd Szolnoki <cpp_at_lenardszolnoki.com>
Subject: Re: [std-discussion] How to force initialization of templated static variable in instantiated template?

On Thu, 21 Jul 2022 19:30:56 +0000
Marios Staikopoulos via Std-Discussion
<std-discussion_at_lists.isocpp.org> wrote:

> Hi, I am attempting to initialize a static runtime variable that is
> initialized in every class via a member static variable that is
> in-turn initialized via a static dispatcher function.
>
> For the most part, everything works - but I have encountered an
> unexpected caveat - the static variable of a template is not
> initialized on template instantiation, only on access of the internal
> variable. This is something I want to be done automatically so that
> you do not have to do any additional instantiation work for every
> possible template variant used in code...
>
> An example of this attempt can be seen here:
> https://godbolt.org/z/Mb3hr576z
>
> Currently, the only two ways I can think of are shown in the example
> below: (1) manually touch the static member in any code
> (2) same as 1, but we simply touch it in the default constructor
>
> However, both examples require code modification *outside* of the
> STATIC_BODY macro. I would like to constrain it to be within if
> possible.
>
> Is anyone aware of any techniques/compiler that can be used to force
> to initialize templated statics on initialization?

Hi,

I'm not sure what exactly is your goal, but here is my approach, although it changes the interface.

https://godbolt.org/z/9ve7saerM

Here I don't have dispatchers, I just define the body inline that I want to run before main on instantiation of the type. It requires static inline variables, therefore C++17. I had a similar approach for solving it with your interface, but MSVC didn't cooperate.

Cheers,
Lénárd
--
Std-Discussion mailing list
Std-Discussion_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2022-07-21 22:25:44