Modern C++ Singleton Template I recently read about the C++17 static inline member declaration and thought that this will make templates a little bit cleaner, since static members can now be initialized inside a templated class.

Because of this I wanted to create a neat little Singleton template (since it is the perfect example where static members are needed).

Now to my questions: Is there something I potentially missed, i.e. are there possibilities to create copies of a derived Singleton? Is it a good idea to use CRTP for a Singleton in general? What a about the move constructor, do I need to handle it as well?

Here is the template:

template &lt; typename T &gt;
class Singleton {
  public:
    static T&amp; GetInstance() {
      static MemGuard g; // clean up on program end
      if (!m_instance) {
        m_instance = new T(); 
      }
      return *m_instance;
    }

    Singleton(const Singleton&amp;) = delete;
    Singleton&amp; operator= (const Singleton) = delete;

  protected:
    Singleton() { };
    virtual ~Singleton() { }

  private:
    inline static T * m_instance = nullptr;

    class MemGuard {
      public: 
        ~MemGuard() {
          delete m_instance;
          m_instance = nullptr;
        }
    };
};


And here a possible derived type: 

class Test final : public Singleton&lt;Test&gt; {
  friend class Singleton&lt;Test&gt;;
  public:
    void TestIt() { };
  private:
    Test() {}  
    ~Test() { /* Test intern clean up */ }
};
Okay so first of the obligatory Singletons are bad practice so you probably shouldn't make it easy to write bad code. 

Ignoring the fact that the class probably shouldn't exist at all we can look at the code.

static T&amp; GetInstance() {
  static MemGuard g; // clean up on program end
  if (!m_instance) {
    m_instance = new T(); 
  }
  return *m_instance;
}


If multiple threads access this instance simultaneously before it is created, you have a data race and m_instance may end up be being constructed multiple times or other kinds of undefined behaviour. You need to add mutex locks around the if block or use std::call_once which is preferred.

As it is supposed to be a singleton you're not supposed to be able to create more instances as the meaning of a singleton is to just have one instance but it appears that it is fully possible to construct multiple instances of Test  simply by creating them as local variables. So this is a design flaw in your template. 

A much better way of creating a singleton is to rely on C++11 Magic Statics (N2660). And simply do this:

class Test{
private:
    Test(); // Disallow instantiation outside of the class.
public:
    Test(const Test&amp;) = delete;
    Test&amp; operator=(const Test &amp;) = delete;
    Test(Test &amp;&amp;) = delete;
    Test &amp; operator=(Test &amp;&amp;) = delete;

    static auto&amp; instance(){
        static Test test;
        return test;
    }
}; 


Which is much easier to write than your code, it's thread safe and fixes the issues with allowing Test to be instantiated. The properties of magic statics guarantee that test will be initialised exactly once the first time the function body is entered by any thread, even in the presence of multiple threads that might otherwise cause a data-race. The instance will be deconstructed when your main() function returns (in the static destruction stage) which makes the whole MemGuard thing unnecessary.