title: global extern pointer to unknown templated type
body: <p><strong>Background:</strong></p>
  <p>I have a game engine where there are several <code>extern</code>ed global pointers to various subsystems for ease-of-access for downstream game applications:</p>
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  
  #include &lt;type_traits&gt;
  
  class JobSystem;
  class FileLogger;
  class Renderer;
  class Console;
  class Config;
  class UISystem;
  class InputSystem;
  class AudioSystem;
  class EngineSubsystem;
  
  class GameBase;
  
  extern JobSystem* g_theJobSystem;
  extern FileLogger* g_theFileLogger;
  extern Renderer* g_theRenderer;
  extern Console* g_theConsole;
  extern Config* g_theConfig;
  extern UISystem* g_theUISystem;
  extern InputSystem* g_theInputSystem;
  extern AudioSystem* g_theAudioSystem;
  extern GameBase* g_theGame;
  extern EngineSubsystem* g_theSubsystemHead;
  
  template&lt;typename GameDerived&gt;
  GameDerived* GetGameAs() noexcept {
      static_assert(std::is_base_of_v&lt;std::remove_cv_t&lt;std::remove_reference_t&lt;std::remove_pointer_t&lt;GameBase&gt;&gt;&gt;, std::remove_cv_t&lt;std::remove_reference_t&lt;std::remove_pointer_t&lt;GameDerived&gt;&gt;&gt;&gt;);
      return dynamic_cast&lt;GameDerived*&gt;(g_theGame);
  }
  </code></pre>
  <p><strong>Problem:</strong></p>
  <p>I'd like to be able to provide a global pointer to the <code>App</code> class but the fact that the class is a template is making it difficult to understand how to implement it.</p>
  <p><strong>Pseudo-code Implementation:</strong></p>
  <p>Class hierarchy structure:</p>
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  #include &lt;type_traits&gt;
  
  class GameBase { /*...*/ }
  class Game : public GameBase { /*...*/ }
  
  template&lt;typename GameType&gt;
  class App {
  static_assert(std::is_base_of_v&lt;GameBase, GameType&gt;, &quot;Template type GameType not derived from GameBase.&quot;);
  
  public:
  
  static void CreateApp() {
      if(_theApp) {
          return;
      }
      _theApp = new App&lt;T&gt;();
  }
  
  static void DestroyApp() {
      if(!_theApp) {
          return;
      }
      delete _theApp;
      _theApp = nullptr;
  }
  
  //...
  
  private:
  
      void SetupSubsystemPointers() {
          
          //...
          
          _theRenderer = std::make_unique&lt;Renderer&gt;();
          g_theRenderer = _theRenderer.get();
          
          //...
          
          _theGame = std::make_unique&lt;GameType&gt;();
          g_theGame = _theGame.get();
  
          //...This does not work:
          //g_theApp = this;
      }
  
      std::unique_ptr&lt;JobSystem&gt; _theJobSystem{};
      std::unique_ptr&lt;FileLogger&gt; _theFileLogger{};
      std::unique_ptr&lt;Config&gt; _theConfig{};
      std::unique_ptr&lt;Renderer&gt; _theRenderer{};
      std::unique_ptr&lt;Console&gt; _theConsole{};
      std::unique_ptr&lt;InputSystem&gt; _theInputSystem{};
      std::unique_ptr&lt;UISystem&gt; _theUI{};
      std::unique_ptr&lt;AudioSystem&gt; _theAudioSystem{};
      std::unique_ptr&lt;GameType&gt; _theGame{};
  
      //I would really like this to be a std::unique_ptr.
      static inline App&lt;GameType&gt;* _theApp{};
  
  }
  </code></pre>
  <p>Here's where I'm running in to trouble implementing the global pointer to the App class.</p>
  <p>Common header:</p>
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  
  #include &lt;type_traits&gt;
  
  class JobSystem;
  class FileLogger;
  class Renderer;
  class Console;
  class Config;
  class UISystem;
  class InputSystem;
  class AudioSystem;
  class EngineSubsystem;
  
  class GameBase;
  
  extern JobSystem* g_theJobSystem;
  extern FileLogger* g_theFileLogger;
  extern Renderer* g_theRenderer;
  extern Console* g_theConsole;
  extern Config* g_theConfig;
  extern UISystem* g_theUISystem;
  extern InputSystem* g_theInputSystem;
  extern AudioSystem* g_theAudioSystem;
  extern GameBase* g_theGame;
  
  // WHAT DO I NEED TO DO TO GET SOMETHING LIKE THE FOLLOWING TO WORK?
  //extern App* g_theApp;
  
  //I tried
  //template&lt;typename GameType&gt;
  //App&lt;GameType&gt;* g_theApp but compilation failed with an undefined variable error or template vomit errors.
  //extern GameType* g_theGame; //This would be nice to have. It would replace the above GameBase pointer and the below GetGameAs function
  
  extern EngineSubsystem* g_theSubsystemHead;
  
  template&lt;typename GameDerived&gt;
  GameDerived* GetGameAs() noexcept {
      static_assert(std::is_base_of_v&lt;std::remove_cv_t&lt;std::remove_reference_t&lt;std::remove_pointer_t&lt;GameBase&gt;&gt;&gt;, std::remove_cv_t&lt;std::remove_reference_t&lt;std::remove_pointer_t&lt;GameDerived&gt;&gt;&gt;&gt;);
      return dynamic_cast&lt;GameDerived*&gt;(g_theGame);
  }
  </code></pre>
  <p>Common implementation:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;Engine/Core/EngineCommon.hpp&quot;
  
  JobSystem* g_theJobSystem = nullptr;
  FileLogger* g_theFileLogger = nullptr;
  Renderer* g_theRenderer = nullptr;
  Console* g_theConsole = nullptr;
  Config* g_theConfig = nullptr;
  UISystem* g_theUISystem = nullptr;
  InputSystem* g_theInputSystem = nullptr;
  AudioSystem* g_theAudioSystem = nullptr;
  GameBase* g_theGame = nullptr;
  EngineSubsystem* g_theSubsystemHead = nullptr;
  
  //DOES NOT WORK
  //App* g_theApp = nullptr;
  
  </code></pre>