title: How are ".h" header files able to "see" the contents of their corresponding ".cpp" files?
body: <p>Let me first begin by providing a little background to explain the motivation for this question:</p>
  <p>I've always been under the impression that a <code>#include</code> directive in C++ has the net effect of inserting the contents of the included file at the point where the directive appears.</p>
  <p>For example, when the preprocessor encounters <code>#include &quot;myInclude.h</code>, it would simply replace that directive with the content found in the file <code>myInclude.h</code></p>
  <p>You can imagine my surprise when, earlier today, I was editing some code in Microsoft Visual Studio 2019 and the IntelliSense feature was flagging a class declaration in a header as undefined <strong>until</strong> I included that header in it's corresponding .cpp file.</p>
  <p>Here are the specific files involved:</p>
  <pre><code>// myInclude.h
  #pragma once
  class MyDlg : public CDialogImpl&lt;MyDlg&gt;
  {
  // Omitted code
  };
  </code></pre>
  <pre><code>// myInclude.cpp
  #include &quot;stdafx.h&quot;
  #include &quot;myInclude.h&quot; // &lt;-- if this line is here, intellisense in myInclude.h is happy
                         //     but if commented out, CDialogImpl is undefined
  
  // Omitted implementation code
  </code></pre>
  <pre><code>// stdafx.h
  #include &lt;atlbase.h&gt;
  #include &lt;atlcom.h&gt;
  #include &lt;atlwin.h&gt;
  //... ommited headers
  </code></pre>
  <p>(<code>CDialogImpl</code> is a template class defined in one of the ATL headers referenced by <code>stdafx.h</code>)</p>
  <p>So what is going on here?  From the context of <code>myInclude.cpp</code>, I can see how everything works -- <code>CDialogImpl</code> is being brought in via <code>stdafx.h</code>, and the declaration of <code>MyDlg</code> is being brought in via <code>myInclude.h</code>; But how is <code>myInclude.h</code> able to resolve <code>CDialogImpl</code> by bringing the include into <code>myInclude.cpp</code>?</p>
  <p>I prefer to keep my includes in <code>.cpp</code> files for performance reasons so this is a great feature to know about...but I want to make sure I understand what is happening before I begin using it everywhere.</p>
  <p>Questions:</p>
  <ol>
  <li>Is <code>myInclude.h</code> really somehow able to &quot;see&quot; the <code>myInclude.cpp</code> file directives?</li>
  <li>Or is this a weird artifact of how IntelliSense works?</li>
  <li>If it isn't related to IntelliSense, is it standardized C++ behavior that will work in other compilers (g++, etc.)?</li>
  </ol>