title: C++ modules templates in union undefined struct - VC 2017
body: <p>I'm trying to use Visual Studio 2017's experimental module stuff with templates in the module. Here's simplified example of a module I'm trying to make</p>
  
  <pre><code>// MyModule.ixx
  module MyModule;
  
  export
  {
      template&lt;typename T&gt;
      struct Struct_1
      {
          T value;
      }
  
      template&lt;typename T&gt;
      struct Struct_2
      {
          union
          {
              T data[1];
              Struct_1&lt;T&gt; x;
          };
      }
  }
  </code></pre>
  
  <p>And here's how I'm trying to use the module and template structs</p>
  
  <pre><code>// main.cpp
  import std.core;
  import MyModule;
  
  int main(int argc, char* argv[])
  {
      Struct_2&lt;float&gt; s;
      std::cout &lt;&lt; s.data[0];
  
      return 0;
  }
  </code></pre>
  
  <p>Compiling gives the following: <code>error C2079: 'Struct_2::x' uses undefined struct 'Struct_1&lt;T&gt;</code></p>
  
  <p>I understand that modules are an experimental feature, however, when not using modules, defining the structs like this works fine. It says <code>Struct_1&lt;T&gt;</code> is undefined, so does that mean I need to somehow explicitly instantiate the templates?</p>
  
  <p>My compiler command line looks like this: </p>
  
  <p><code>cl /utf-8 /experimental:module MyModule.ixx /std:c++latest /W3 /Zi /MDd /EHsc main.cpp /link /OUT:test.exe /INCREMENTAL:NO</code></p>
  
  <p>Any help is appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I think this is some sort of MSVC compiler bug. I compiled the same code using Clang and it works fine. The Clang module file looks the same as above except the file extension is .cppm and at the top instead it has: </p>
  
  <p><code>export module MyModule;</code></p>
  
  <p>My compiler command line looks like:</p>
  
  <pre><code>clang++ -fmodules-ts --precompile MyModule.cppm -o MyModule.pcm
  clang++ -fmodules-ts -fprebuilt-module-path=. main.cpp
  </code></pre>
  
  <p>It's probably a good idea to just wait for the MSVC compiler to mature a little more before using modules, but if anybody knows a way to get around this for the time being, I would be interested in seeing it.</p>
  
  <p>Thanks</p>