title: C++ Modules Forward Declaring Entity from Another Module
body: <p>I have recently been trying to convert a codebase to C++20 modules using GCC 11.  However, I got stuck on the following situation.  First, here is how it was done using headers:</p>
  <p>A.h</p>
  <pre class="lang-cpp prettyprint-override"><code>class B;
  
  class A {
      public:
          void f(B&amp; b);
  };
  </code></pre>
  <p>A.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;A.h&quot;
  #include &quot;B.h&quot;
  
  void A::f(B&amp; b)
  {
      // do stuff with b
  }
  </code></pre>
  <p>(the contents of B.h is not important here)</p>
  <p>The thing to note is the forward declaration of B.  Not everybody using A should care about B, so I used a forward declaration to stop recompiling from happening.  With headers, this situation works perfectly.</p>
  <p>The issue is when trying to convert this code to modules.  The main issue is that entities are tied to the module they are declared in, so forward declaring in A.h is impossible.  I tried forward declaring in the global module, but then the compiler still complained that the definition of B was in a different module than its declaration.  I also tried having a third module that just contained the forward declaration of B, but this was still declaring and defining B in two different modules.  So, my main question is: <strong>how can I forward declare something from a module outside of it?</strong>  I would also be satisfied with a way that ends up producing the same effect: that users of A don't need to be recompiled when B changes.</p>
  <p>While searching, I found a few places talking about similar situations, but they all didn't work for some reason.  The reasons they didn't work:</p>
  <ul>
  <li>Some said to have a module with the forward declarations.  As I said above, this doesn't work.</li>
  <li>Some said to use proclaimed ownership declarations.  However, they were removed from the final C++ standard.</li>
  <li>Some said to use module partitions.  However, this only works if A and B are in the same module.  A and B shouldn't be in the same module, so this doesn't work.</li>
  </ul>
  <p><strong>Edit</strong>: in response to a comment, here are the details for a few of the things I have tried:</p>
  <p><em>Attempt 1: Forward declare B in A.mpp</em></p>
  <p>A.mpp</p>
  <pre class="lang-cpp prettyprint-override"><code>export module A;
  
  class B;
  
  export class A {
      public:
          void f(B&amp; b);
  };
  </code></pre>
  <p>B.mpp</p>
  <pre class="lang-cpp prettyprint-override"><code>export module B;
  
  export class B {};
  </code></pre>
  <p>A.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>module A;
  
  import B;
  
  void A::f(B&amp; b)
  {
      // do stuff with b
  }
  </code></pre>
  <p>When doing this, gcc errors with</p>
  <pre><code>A.cpp:4:11: error: reference to ‘B’ is ambiguous
      4 | void A::f(B&amp; b)
        |           ^
  In module B, imported at A.cpp:2:
  B.mpp:3:14: note: candidates are: ‘class B@B’
      3 | export class B {};
        |              ^
  In module A, imported at A.cpp:1:
  A.mpp:3:7: note:                 ‘class B@A’
      3 | class B;
  </code></pre>
  <p><em>Attempt 2: Forward declare in new module</em></p>
  <p>B_decl.mpp</p>
  <pre class="lang-cpp prettyprint-override"><code>export module B_decl;
  
  export class B;
  </code></pre>
  <p>A.mpp</p>
  <pre class="lang-cpp prettyprint-override"><code>export module A;
  
  import B_decl;
  
  export class A {
      public:
          void f(B&amp; b);
  };
  </code></pre>
  <p>B.mpp</p>
  <pre class="lang-cpp prettyprint-override"><code>export module B;
  
  import B_decl;
  
  class B {};
  </code></pre>
  <p>A.mpp</p>
  <pre class="lang-cpp prettyprint-override"><code>module A;
  
  import B;
  
  void A::f(B&amp; b)
  {
      // do stuff with b
  }
  </code></pre>
  <p>When doing this, gcc errors with</p>
  <pre><code>B.mpp:5:14: error: cannot declare ‘class B@B_decl’ in a different module
      5 | class B {};
        |              ^
  In module B_decl, imported at B.mpp:3:
  B_decl.mpp:3:14: note: declared here
      3 | export class B;
  </code></pre>
  <p><em>Attempt 3: Forward declare in header, define in module</em></p>
  <p>B_decl.h</p>
  <pre class="lang-cpp prettyprint-override"><code>class B;
  </code></pre>
  <p>A.mpp</p>
  <pre class="lang-cpp prettyprint-override"><code>module;
  
  #include &quot;B_decl.h&quot;
  
  export module A;
  
  export class A {
      public:
          void f(B&amp; b);
  };
  </code></pre>
  <p>B.mpp</p>
  <pre class="lang-cpp prettyprint-override"><code>module;
  
  #include &quot;B_decl.h&quot;
  
  export module B;
  
  class B {};
  </code></pre>
  <p>A.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>module A;
  
  import B;
  
  void A::f(B&amp; b)
  {
      // do stuff with b
  }
  </code></pre>
  <p>When doing this, gcc errors with</p>
  <pre><code>B.mpp:7:7: error: cannot declare ‘class B’ in a different module
      7 | class B {};
        |       ^
  In file included from B.mpp:3:
  B_decl.h:1:7: note: declared here
      1 | class B;
  </code></pre>