title: What's wrong with std::valarray's operator*?
body: <p>Consider the following MCVE, where I have two value arrays where <code>w</code> is two times <code>v</code> (<a href="https://godbolt.org/z/JkXPNN" rel="nofollow noreferrer">try it out here</a>):</p>
  
  <pre><code>#include &lt;valarray&gt;
  
  using namespace std;
  
  int main() {
    valarray&lt;int&gt; v { 1, 2, 3 };
  
    for ([[maybe_unused]] auto x : v) {} // Ok
  
    auto w = v * 2;     // Leads to failure in loop below
    //valarray&lt;int&gt; w = v * 2; // Works
    //auto w = v*=2;      // Works
    //auto w = v; w *= 2; // Works
  
    for ([[maybe_unused]] auto x : w) {} // Failure here
  }
  </code></pre>
  
  <p>This example fails to compile with clang and gcc at the last loop with (gcc output here):</p>
  
  <pre><code>error: no matching function for call to 'begin(std::_Expr&lt;std::__detail::_BinClos&lt;std::__multiplies, std::_ValArray, std::_Constant, int, int&gt;, int&gt;&amp;)'
  </code></pre>
  
  <p>The source of the problem seems to be the decuced type of <code>v * 2</code> (I assume that because explicitly writing down the type works, so some implicit conversion seems to be taking place).</p>
  
  <p>Looking at the <a href="https://en.cppreference.com/w/cpp/numeric/valarray/operator_arith3#Note" rel="nofollow noreferrer">reference notes</a>, it seems that <code>operator*</code> may return something different than <code>std::valarray&lt;T&gt;</code>.
  I don't understand the reason for this but more puzzling is that <a href="https://en.cppreference.com/w/cpp/numeric/valarray/operator_arith2#Notes" rel="nofollow noreferrer">the same seem to apply to <code>operator*=</code></a>, except that here my <code>auto</code> assignment works. I would expect the return values of <code>operator*=</code> and <code>operator*</code> to be the same here (delta the reference).</p>
  
  <p>So my questions are:</p>
  
  <ul>
  <li>Is this an implementation issue/bug? Or am I missing something?</li>
  <li>What's the rationale behind the <a href="https://en.cppreference.com/w/cpp/numeric/valarray/operator_arith3#Note" rel="nofollow noreferrer">reference notes</a> (e.g. why can the operators return something different that may not work with <code>std::begin</code>/<code>std::end</code>)?</li>
  </ul>
  
  <p><em>(Note: I tagged this question c++11, but it seems to apply to all versions up to 17 as well)</em></p>