Magic Static in Singleton Template I recently was reading an article labeled &quot;Modern C++ Singleton Template&quot;. The proposed solution used C++11 feature of Magic Statics (N2660). I wanted to adapt it for an own project in
&quot;freestanding&quot; (fno-hosted) environment.
My code looks as follows:
Singleton.hpp
namespace std
{
    template &lt;typename T&gt;
    class Singleton
    {
        public:
            // = initialization and termination methods
            Singleton( const Singleton&amp; )   = delete;   // copy constructor
            Singleton(       Singleton&amp;&amp; )  = delete;   // move constructor
            Singleton&amp; operator=( const Singleton&amp; ) = delete;  // assignment operator
            Singleton&amp; operator=(       Singleton&amp;&amp;) = delete;  // copy move operator
            // = accessor methods.
            static T&amp; getInstance();
        protected:
             Singleton() {};                            // default constructor
            ~Singleton() {};                            // default destructor
             struct Token {};
        private:
    }; // end template class T

    template &lt;typename T&gt;
    T&amp; Singleton&lt;T&gt;::getInstance()
    {
        static T    __singleInstance( Token{} );    
        return( __singleInstance );
    } // end public method 'getInstance()'
} // end namespace std

Test.hpp
#include &quot;libs/libc++/singleton.hpp&quot;        // declaration of template class 'Singleton'

class Test final : public TestSingelton&lt;Test&gt;
{
    public:
    Test(TestToken);
   ~Test();

    void use();
};

Test.cpp
Test::Test(TestToken)
{
    logTraceEvent_m( loging to file );
}

Test::~Test()
{
    logTraceEvent_m( loging to file );
}

void 
Test::use()
{
    logTraceEvent_m( loging to file );
}

Problem
In hosted environment (Linux, GCC 7.5.0) it works as presented in the article refereed to above.
The static variable is initialized only once; the constructor of the derived class is called only once.
Entering main()
Entering a()
constructed
in use
Entering b()
in use
Leaving main()
destructed

However, in my &quot;-ffreestandig&quot; environment it is not working.
&quot;freestanding&quot; = kernel without any standard headers; without any libraries!
Static variable (&quot;__singleInstance&quot;) is initialized on every call to &quot;Test::getInstance()&quot;.
______ TRACE: constructor = Test::Test(TestSingelton&lt;Test&gt;::TestToken), ...
______ TRACE: use = void Test::use(), file = test/Test.cpp, line = 37.
______ TRACE: constructor = Test::Test(TestSingelton&lt;Test&gt;::TestToken), ...
______ TRACE: use = void Test::use(), file = test/Test.cpp, line = 37.

What I'm doing wrong?
Is there any advice in which direction I have to investigate?

compiler options?
-W -Wall -pedantic -ffreestanding -std=gnu++17 -fno-PIC -fno-exceptions -fno-rtti
-fno-use-cxa-atexit -march=native -m64 -mcmodel=kernel -mno-red-zone -mno-sse3 -mno-ssse3
-mno-sse4.1 -mno-sse4.2 -mno-sse4 -mno-sse4a -mno-3dnow -mno-avx -mno-avx2
runtime environment, that is missing something
Your test code doesn't match your test output. &quot;Test.cpp&quot; isn't even valid C++ code!
How does your massive amount of code do anything significantly different from the following eight lines?
template&lt;class T&gt;
struct Singleton {
    struct Token {};
    static T&amp; getInstance() {
        static T instance(Token{});
        return instance;
    }
};

Example usage:
struct Widget { explicit Widget(Singleton&lt;Widget&gt;::Token); };
Widget&amp; example = Singleton&lt;Widget&gt;::getInstance();

As for why the &quot;thread-safe statics&quot; feature doesn't work on your freestanding platform, I would suspect it's because your freestanding platform doesn't support thread-safe statics. :)  Check the documentation for your toolchain to see how static variables are handled on that particular platform.