valarray with arithmetic operations return type : <p>When I write a simple arithmetic expression with <code>valarray</code> and assign the result to <code>auto</code> I get a segfault when I try to access the result on gcc.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;valarray&gt;
using std::ostream; using std::valarray;
ostream&amp; operator&lt;&lt;(ostream&amp;os, const valarray&lt;double&gt;&amp;vs) {
    os &lt;&lt; "[";
    for(auto&amp;v : vs) os &lt;&lt; v &lt;&lt; " ";
    return os &lt;&lt; "]";
}
int main() {
    valarray&lt;double&gt; a{ 1.0, 2.0, 3.0, 4.0 };
    std::cout &lt;&lt; "a: " &lt;&lt; a &lt;&lt; "\n";
    valarray&lt;double&gt; b{ 2.0, 4.0, 6.0, 8.0 };
    std::cout &lt;&lt; "b: " &lt;&lt; b &lt;&lt; "\n";
    valarray&lt;double&gt; c{ 2.0, 1.5, 0.5, 0.25 };
    std::cout &lt;&lt; "c: " &lt;&lt; c &lt;&lt; "\n";
    valarray&lt;double&gt; x = ( a + b ) / 2;
    std::cout &lt;&lt; "x: " &lt;&lt; x &lt;&lt; "\n";
    // this still works:
    auto y = ( a + b ) / 2;
    // The following will result in a segfault:
    std::cout &lt;&lt; "y:" &lt;&lt; y &lt;&lt; "\n";
}
</code></pre>

<p>The <a href="http://www.cplusplus.com/reference/valarray/valarray/operators/" rel="noreferrer">reference</a> says that the implementation may choose that the return type of the arithmetic operation overloads may not be a <code>valarray</code>-value but something that "behaves like it":</p>

<blockquote>
  <p>The operators returning a valarray by value are allowed to return an object of a different type instead. Such a type is required to be implicitly convertible to valarray and be supported as argument for all functions taking valarray&amp; arguments. This allows copy-on-write implementations.</p>
</blockquote>

<p>Well, my <code>operator&lt;&lt;</code> should call for that "implicit conversion", shouldnt it?</p>

<p>So why do I get a segfault?</p>

<pre><code>$ ./valarray01.cpp.x
a: [1 2 3 4 ]
b: [2 4 6 8 ]
c: [2 1.5 0.5 0.25 ]
x: [1.5 3 4.5 6 ]
Segmentation fault (core dumped)
</code></pre>

<blockquote>
  <p>gcc version 6.2.0 20160901 (Ubuntu 6.2.0-3ubuntu11~14.04) </p>
</blockquote>

<p>I got sceptical when I tried <em>clang</em> (on linux, so probably gcc's stdlib) and... it works:</p>

<blockquote>
  <p>clang version 3.9.1-svn288847-1~exp1 (branches/release_39)</p>
</blockquote>

<pre><code>$ ./valarray01.cpp.x
a: [1 2 3 4 ]
b: [2 4 6 8 ]
c: [2 1.5 0.5 0.25 ]
x: [1.5 3 4.5 6 ]
y:[1.5 3 4.5 6 ]
</code></pre>

<p>Well, before I file a gcc-bug... am I doing something wrong? Is my <code>auto</code> evil? Or is it really gcc?</p>
