Home > Article > Backend Development > How to Force Initialization of a Static Member in C Without Explicit Referencing?
How to Force Initialization of a Static Member
An often overlooked behavior in C is that static members are not guaranteed to be initialized automatically. One may anticipate that the member is initialized with the first instantiation of the concrete class. However, as the quote from the standard suggests, this occurs only when the static member is actively utilized:
"* in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist."
The Problem
Consider the following code:
<code class="cpp">template <class D> char register_() { return D::get_dummy(); // static function } template <class D> struct Foo { static char const dummy; }; template <class D> char const Foo<D>::dummy = register_<D>(); // static member initialized with `register_<D>()` struct Bar : Foo<Bar> { static char const get_dummy() { return 42; } };</code>
Intuitively, one might expect dummy to be initialized upon the instantiation of Bar. However, this does not happen.
The Question
How can one force the initialization of dummy without requiring an instance of Bar or Foo? Additionally, the solution should not necessitate explicit referencing of the member by the user of Foo.
Potential Solutions
Solution 1 (Minimal Impact on Derived Class)
The following modification to Bar's get_dummy() method will force the initialization of dummy:
<code class="cpp">static char const get_dummy() { (void)dummy; return 42; }</code>
Solution 2 (No Modification to Derived Class)
This solution utilizes template metaprogramming:
<code class="cpp">template<typename T, T> struct value { }; template<typename T> struct HasStatics { static int a; // we force this to be initialized typedef value<int&, a> value_user; }; template<typename T> int HasStatics<T>::a = /* whatever side-effect you want */ 0;</code>
Or, it can be done without introducing any additional members:
<code class="cpp">template<typename T, T> struct var { enum { value }; }; template<typename T> struct HasStatics { static int a; // we force this to be initialized static int b; // and this char user :var<int&, a>::value, :var<int&, b>::value; }; template<typename T> int HasStatics<T>::a = /* whatever side-effect you want */ 0; template<typename T> int HasStatics<T>::b = /* whatever side-effect you want */ 0;</code>
The above is the detailed content of How to Force Initialization of a Static Member in C Without Explicit Referencing?. For more information, please follow other related articles on the PHP Chinese website!