Date: Thu, 21 Jul 2022 20:06:23 +0000
After taking a small coffee break, I found a promising solution.
Since the static variable `StaticInit` in the example is an empty struct, and should only occupy 1 byte, we can have a non-static member that also copies the value, this in-turn enforces the initialization of the static variable.
EG:
#define STATIC_BODY() \
static StaticInit __sinit;
Can turn into this:
#define STATIC_BODY() \
static StaticInit __sinit; \
StaticInit _sinit = __sinit;
I am still investigating the size/performance implications of this technique if there are any - and see how the compiler optimizes it.
So far, however, everything looks golden...
The size of the structures remains unchanged with `sizeof()` returning 1 even with the empty struct member, and the generated constructors (using clang and debug) look identical with or without the empty local;
Currently it seems that there are no adverse side effects, which basically gives me the exact behavior I am looking for.
From: Marios Staikopoulos
Sent: Thursday, July 21, 2022 12:31 PM
To: std-discussion_at_[hidden]<mailto:std-discussion_at_[hidden]>
Subject: How to force initialization of templated static variable in instantiated template?
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?
Since the static variable `StaticInit` in the example is an empty struct, and should only occupy 1 byte, we can have a non-static member that also copies the value, this in-turn enforces the initialization of the static variable.
EG:
#define STATIC_BODY() \
static StaticInit __sinit;
Can turn into this:
#define STATIC_BODY() \
static StaticInit __sinit; \
StaticInit _sinit = __sinit;
I am still investigating the size/performance implications of this technique if there are any - and see how the compiler optimizes it.
So far, however, everything looks golden...
The size of the structures remains unchanged with `sizeof()` returning 1 even with the empty struct member, and the generated constructors (using clang and debug) look identical with or without the empty local;
Currently it seems that there are no adverse side effects, which basically gives me the exact behavior I am looking for.
From: Marios Staikopoulos
Sent: Thursday, July 21, 2022 12:31 PM
To: std-discussion_at_[hidden]<mailto:std-discussion_at_[hidden]>
Subject: How to force initialization of templated static variable in instantiated template?
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?
Received on 2022-07-21 20:06:27