Search code examples
c++templatesconstructorstaticstatic-constructor

Emulating static constructors for templated classes


I would like to have a templated class with a static data member, and initialize it by emulating a "static constructor." For a non-templated class, this has already been answered (see static constructors in C++? I need to initialize private static objects and What is a static constructor?). However, none of the answers seem to work for a templated class.

The following is an example that tries to adapt the "static constructor" idiom from the previous answers to a templated class. (Note that the example is simply initializing an int and could be written without such constructors; however, I require a general solution.)

#include <iostream>

struct Foo
{
    static int x;
    static struct init
    {
        init()
        {
            std::cout << "Initializing Foo..." << std::endl;
            x = 1;
        }
    } initializer;
};
int Foo::x;
Foo::init Foo::initializer;

template<int N>
struct Bar
{
    static int x;
    static struct init
    {
        init()
        {
            std::cout << "Initializing Bar..." << std::endl;
            x = N;
        }
    } initializer;
};

template<int N>
int Bar<N>::x;
template<int N>
typename Bar<N>::init Bar<N>::initializer;

int main()
{
    std::cout << Foo::x << std::endl;
    std::cout << Bar<1>::x << std::endl;
    return 0;
}

This outputs:

Initializing Foo...
1
0

But I expected it to output:

Initializing Foo...
Initializing Bar...
1
1

Is this an example of the "static initialization order fiasco?"


Solution

  • I have found a clean solution that works for any data type. Since the assignment operation inside a template is evaluated when the compiler comes across a specific Bar<N>::x to instantiate, we can write:

    template<int N>
    int Bar<N>::x = init<N>();
    

    where init() is a function templated on N that returns an int. Additionally, init() will only be called once for each value of N that the compiler instantiates.

    As a more useful example, here I initialize a static std::array according to some arbitrary function:

    #include <iostream>
    #include <array>
    
    template<int N>
    struct Foo
    {
        static std::array<double,N> x;
    };
    
    template<int N>
    std::array<double,N> init()
    {
        std::array<double,N> y;
        for (int i=0; i<N; ++i) {
            y[i] = (double)(i*i+i)/N;
        }
        return y;
    }
    
    template<int N>
    std::array<double,N> Foo<N>::x = init<N>();
    
    int main()
    {
        const int N = 10;
        for (int i=0; i<N; ++i) {
            std::cout << Foo<N>::x[i] << std::endl;
        }
        return 0;
    }