title: C++ unqualified name lookup: different structure size in different cpp's leading to operator new allocating less memory than constructor processes?
body: <p>Here is the example:</p>
  
  <p><code>Main.cpp</code>:</p>
  
  <pre><code>#include "MooFoobar.h"
  #include "MooTestFoobar.h"
  
  #include "FoobarUser.h"
  
  namespace moo::test::xxx {
      struct X
      {
          void* operator new(const size_t size);
  
          FoobarUser m_User;
      };
  
      void* X::operator new(const size_t size)
      {
          printf("Allocated size: %zd\n", size);
          return malloc(size);
      }
  } // namespace moo::test::xxx
  
  int main()
  {
      new moo::test::xxx::X;
      printf("Actual size: %zd, member size: %zd\n", sizeof(moo::test::xxx::X), sizeof(moo::test::xxx::FoobarUser));
      return 0;
  }
  </code></pre>
  
  <p><code>MooFoobar.h</code>:</p>
  
  <pre><code>namespace moo {
      struct Foobar
      {
          char m_Foo[64];
      };
  } // namespace moo
  </code></pre>
  
  <p><code>MooTestFoobar.h</code>:</p>
  
  <pre><code>namespace moo::test {
      struct Foobar
      {
          char m_Foo[32];
      };
  } // namespace moo::test
  </code></pre>
  
  <p><code>FoobarUser.h</code>:</p>
  
  <pre><code>#include "MooFoobar.h"
  
  namespace moo::test::xxx {
      struct FoobarUser
      {
          FoobarUser();
          ~FoobarUser();
  
          Foobar m_Foobar;
      };
  } // namespace moo::test::xxx
  </code></pre>
  
  <p><code>FoobarUser.cpp</code>:</p>
  
  <pre><code>#include "FoobarUser.h"
  #include &lt;cstdio&gt;
  
  moo::test::xxx::FoobarUser::FoobarUser()
      : m_Foobar()
  {
      printf("FoobarUser constructor, size: %zd\n", sizeof(*this));
  }
  
  moo::test::xxx::FoobarUser::~FoobarUser()
  {}
  </code></pre>
  
  <p>So what is going on here: depending on order of includes unqualified name is resolved in different types and in <code>FoobarUser.cpp</code> we get size <code>64</code>, in <code>Main.cpp</code> we get size <code>32</code>. Not only <code>sizeof</code> is different - <code>operator new</code> is called with incorrect (<code>32</code>) size, but constructor will initialize size of <code>64</code>, those leading to memory corruption.</p>
  
  <p>In both clang and msvc the result of this program is:</p>
  
  <pre><code>Allocated size: 32
  FoobarUser constructor, size: 64
  Actual size: 32, member size: 32
  </code></pre>
  
  <p>This sounds very fishy and basically means that unqualified names are no-go if there is name-clash, because depending on include order it may lead to what essentially is incorrect program.</p>
  
  <p>But I can't find any point in the C++ std that would say any of that invalid/ill-formed code. Can anyone help me?</p>
  
  <p>Is it REALLY by standard and not some elaborate mass-compiler issue (though I can't really see how compilers can resolve that situation)?</p>