<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><span class="vcard"><a class="email" href="mailto:richard-llvm@metafoo.co.uk" title="Richard Smith <richard-llvm@metafoo.co.uk>"> <span class="fn">Richard Smith</span></a>
</span> changed
          <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - std::indirect_array::operator= gives wrong result."
   href="https://bugs.llvm.org/show_bug.cgi?id=34515">bug 34515</a>
          <br>
             <table border="1" cellspacing="0" cellpadding="8">
          <tr>
            <th>What</th>
            <th>Removed</th>
            <th>Added</th>
          </tr>

         <tr>
           <td style="text-align:right;">Status</td>
           <td>NEW
           </td>
           <td>RESOLVED
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">CC</td>
           <td>
                
           </td>
           <td>richard-llvm@metafoo.co.uk
           </td>
         </tr>

         <tr>
           <td style="text-align:right;">Resolution</td>
           <td>---
           </td>
           <td>INVALID
           </td>
         </tr></table>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - std::indirect_array::operator= gives wrong result."
   href="https://bugs.llvm.org/show_bug.cgi?id=34515#c2">Comment # 2</a>
              on <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - std::indirect_array::operator= gives wrong result."
   href="https://bugs.llvm.org/show_bug.cgi?id=34515">bug 34515</a>
              from <span class="vcard"><a class="email" href="mailto:richard-llvm@metafoo.co.uk" title="Richard Smith <richard-llvm@metafoo.co.uk>"> <span class="fn">Richard Smith</span></a>
</span></b>
        <pre>OK, so first off, what you're *trying* to do probably results in UB.

[indirect.array.assign]p2 says: "If the indirect_array specifies an element in
the valarray<T> object to which it refers more than once, the behavior is
undefined."

It doesn't seem obvious to me what happens if that is true of the source and
destination indirect_arrays, but the same rule would seem reasonable. That
matches what libstdc++ does -- in its implementation, the relevant pointers are
marked as __restrict__, so aliasing would result in UB.


However, what you're *actually* doing is relying on a MSVC and libstdc++ bug.
Here's how indirect_array's assignment operator is *supposed* to be declared:

  const indirect_array& operator=(const indirect_array&) const;

That's what libc++ does. Despite the UB I mentioned above, that assignment
(probably) results in UB, but in practice assigns left-to-right, thus assigning
input[1] (== 1) to input[1], then assigning input[2] (== 2) to input[3], then
assigning input[3] (now == 2) to input[4], then assigning input[4] (now == 2)
to input[6]. And the answer is {1, 2, 2, 2}.


The MSVC / libstdc++ bug is that they declare the assignment operator as

  const indirect_array& operator=(const indirect_array&);

(Note the lack of 'const'.) Your code does not call that function. Instead, it
calls

  const indirect_array& operator=(const valarray&) const;

... after converting result2 to a valarray<int> (which reads all the elements
at once).


So your code probably has UB, and in any case it's the other two
implementations that are wrong :)</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>