title: std::string assignment issue in VS2019, where it is working fine in VS2015
body: <p>I am having an issue std::string assignment with the below code in VS2019, where it is working fine in VS2015.
  I am using <em>Visual Studio 2019 (v142)</em> as Platform Toolset in VS2019  and <em>Visual Studio 2015 (v140)</em> as Platform Toolset in VS2015.</p>
  <pre><code>class Test
  {
  public:
      Test()
      {
          m_DisplayName = &quot;&quot;;
          m_Type = &quot;&quot;;
      }
      std::string m_DisplayName;
      std::string m_Type;
  };
  
  void CMFCApplication2Dlg::TestFunction()
  {
      // Sample Data setting in vector
      std::vector&lt;Test&gt; vTest;
      Test obj;
      obj.m_DisplayName = &quot;Nam1gd&quot;;
      obj.m_Type = &quot;t1&quot;;
      vTest.emplace_back(obj);
  
      Test obj2;
      obj2.m_DisplayName = &quot;Nam2&quot;;
      obj2.m_Type = &quot;t2&quot;;
      vTest.emplace_back(obj2);
  
      VARIANT vtResult_o;
      VariantInit(&amp;vtResult_o);
      vtResult_o.vt = VT_ARRAY | VT_UI1;
      int usrCount = vTest.size();
      ULONG nBufferSize = usrCount * sizeof(Test);
  
      // Define a safe array of usrCount Item and Starting index as 0 
      SAFEARRAYBOUND safeBounds = { nBufferSize, 0 };
  
      //Create the Safe Array passing it the bounds 
      SAFEARRAY* pSafeArray = SafeArrayCreate(VT_UI1, 1, &amp;safeBounds);
      Test* vTestArray = nullptr;
      SafeArrayAccessData(pSafeArray, (void**)&amp;vTestArray);
  
      // Data setting to Safe array
      for (int nIdx = 0; nIdx &lt; usrCount; nIdx++)
      {
          // ******VALUES ASSIGNING CORRECTLY in VS 2015 ******/
          // ******JUNK VALUE COMES IN THIS ASSIGNMENT in VS 2019 ******/
          vTestArray[nIdx].m_DisplayName = vTest[nIdx].m_DisplayName;
          vTestArray[nIdx].m_Type = vTest[nIdx].m_Type;
      }
  
      /* decrement the lock count*/
      SafeArrayUnaccessData(pSafeArray);
      vtResult_o.parray = pSafeArray;
  }
  </code></pre>
  <p>Since the string value in the <code>vTest[nIdx].m_DisplayName</code> is small it is stored in the small buffer in <code>s._Bx._Buf</code>. In VS2015 while assigning, the small buffer in the source string is copied to the destination string's small buffer.</p>
  <pre><code> for (int nIdx = 0; nIdx &lt; usrCount; nIdx++)
   {
       // ******VALUES ASSIGNING CORRECTLY in VS 2015 ******/
       // ******JUNK VALUE COMES IN THIS ASSIGNMENT in VS 2019 ******/
       vTestArray[nIdx].m_DisplayName = vTest[nIdx].m_DisplayName;
       vTestArray[nIdx].m_Type = vTest[nIdx].m_Type;
   }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/8JMD2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8JMD2.png" alt="Source string " /></a>
  However, in VS2019, it is observed that the during assignment the destination's buffer ( <code>s._Bx._Buf</code>) is updated with junk value and the actual value is updated to the heap <code>s._Bx._ptr</code>.</p>
  <p><em>Ideally since <code>s._Bx</code> is a union either the <code>s._Bx._Buf</code> or <code>s._Bx._ptr</code> should be present.</em> Please find the image below.
  <a href="https://i.stack.imgur.com/9MaW5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9MaW5.png" alt="Destination" /></a></p>
  <p><strong>[Workaround]</strong></p>
  <p>However if I do a std::string casting in VS2019 it is assigning the std::string's small buffer in the source to the small buffer in the destination.</p>
  <pre><code>for (int nIdx = 0; nIdx &lt; usrCount; nIdx++)
  {
      vTestArray[nIdx].m_DisplayName =(std::string)(vTest[nIdx].m_DisplayName);
      vTestArray[nIdx].m_Type = (std::string)(vTest[nIdx].m_Type);
  }
  </code></pre>
  <p>I will appreciate if some one can help me to understand why this difference in VS2015 and VS2019!</p>