Home > Backend Development > C++ > body text

How to Initialize Static Members in Nested Template Functions in C ?

Barbara Streisand
Release: 2024-11-01 16:47:02
Original
800 people have browsed it

 How to Initialize Static Members in Nested Template Functions in C  ?

C Static Member Initialization within Nested Template Functions

Static member initialization can be achieved through nested helper structs, especially in non-templated classes. However, this approach may encounter issues when the enclosing class is parametrized by a template. Specifically, if the helper object is not accessed in the main code, the nested initialization class may not instantiate.

Consider the following simplified example, which illustrates the initialization of a vector:

<code class="cpp">struct A {
    struct InitHelper {
        InitHelper() { A::mA = "Hello, I'm A."; }
    };
    static std::string mA;
    static InitHelper mInit;

    static const std::string& getA(){ return mA; }
};
std::string A::mA;
A::InitHelper A::mInit;


template<class T>
struct B {
    struct InitHelper {
        InitHelper() { B<T>::mB = "Hello, I'm B."; }
    };
    static std::string mB;
    static InitHelper mInit;

    static const std::string& getB() { return mB; }
    static InitHelper& getHelper(){ return mInit; }
};
template<class T>
std::string B<T>::mB;
template<class T>
typename B<T>::InitHelper B<T>::mInit;</code>
Copy after login

When compiled with g 4.4.1, several observations are made:

  • With [1] and [2] commented out, A = Hello, I'm A. This signifies that the initialization works as intended.
  • With [1] uncommented, A = Hello, I'm A. B = This demonstrates that the initialization of B::mB is not triggered automatically.
  • With both [1] and [2] uncommented, A = Hello, I'm A. B = Hello, I'm B. This confirms that access to B::mInit (through [2]) instigates the initialization of B::mB.
  • With [1] commented and [2] uncommented, a segfault occurs, indicating that by directly accessing the helper itself, the initialization of B::mB is attempted prematurely, resulting in an error.

This behavior stems from the compiler's interpretation of the ISO/IEC C 2003 standard (14.7.1). The standard stipulates that:

"Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; 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."

This explains why, in the given example, the static members are not initialized until they are accessed through the getB() or getHelper() methods. Explicitly instantiating the class template or member template in the main function would force their initialization at that point. However, for scenarios where direct access is not feasible, the standard does not specify an elegant workaround for initializing static members within nested template functions.

The above is the detailed content of How to Initialize Static Members in Nested Template Functions in C ?. 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
Latest Articles by Author
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!