Home > Backend Development > C++ > body text

How to Force Initialization of a Static Member in C Without Explicit Referencing?

DDD
Release: 2024-10-31 05:38:01
Original
295 people have browsed it

 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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!