<blockquote>
  <p>We have a performance issue where an expression is first translated to a Boost.uBLAS vector and then evaluated. It makes a difference if the vector creation could be skipped and that the vector_expression is used directly. </p>
</blockquote>

<p>uBLAS provides expression templates (ET) with which temporary object creation is avoided. This is very beneifical if you have multiple elementwise operations in one expression. In such cases, ET has a positiv impact on the runtime performance - although I have to say that it depends on the vector and matrix sizes. Have a look at <a href="https://arxiv.org/pdf/1104.1729.pdf" rel="nofollow noreferrer">this paper</a>.</p>

<blockquote>
  <p>I couldn't find in the Boost.uBLAS documentation if this is allowed.</p>
</blockquote>

<p>It is allowed - otherwise the copy constructors would have been protected or private, see the class <a href="https://github.com/boostorg/ublas/blob/1e8689bb0b316340ef93c2e4c9bfd14ad626d391/include/boost/numeric/ublas/matrix_expression.hpp#L49" rel="nofollow noreferrer">matrix_expression</a> description.</p>

<blockquote>
  <p>In fact the examples in the documentation are with the container
  classes and not with expressions directly. </p>
</blockquote>

<p>True. We need to provide more examples.
However, the ublas <a href="https://www.boost.org/doc/libs/1_72_0/libs/numeric/ublas/doc/vector_expression.html#vector_operations" rel="nofollow noreferrer">documentation</a> shows that the binary vector operation creates an <code>expression_type</code> which holds a reference to the operation and operands. You can use such an instance.</p>

<blockquote>
  <p>Boost.uBLAS uses expression templates which in theory should make the
  case work. The norm_2 function accepts a vector_expression as argument
  which could be a second clue.</p>
</blockquote>

<p>Yes, have a look at the generated assembler in <a href="https://godbolt.org/z/NcE6-8" rel="nofollow noreferrer">compiler explorer</a>. No memory is allocated inside the <code>for</code> loop. So your approach is valid.</p>

<p>You can also write</p>

<pre class="lang-cpp prettyprint-override"><code>// norm_2 uses a temporary expression instance again without memory allocation
d += ublas::norm_2(row1 - row2); 
</code></pre>
Use of vector_expression directly in Boost.uBLAS
<p>We have a performance issue where an expression is first translated to a Boost.uBLAS vector and then evaluated. It makes a difference if the vector creation could be skipped and that the vector_expression is used directly. I couldn't find in the Boost.uBLAS documentation if this is allowed. In fact the examples in the documentation are with the container classes and not with expressions directly. It only mentions that Boost.uBLAS uses expression templates which in theory should make the case work. The norm_2 function accepts a vector_expression as argument which could be a second clue.</p>

<p>A simplified case is like this where the norm between rows of a matrix is calculated:</p>

<pre><code>#include &lt;boost/numeric/ublas/assignment.hpp&gt;
#include &lt;boost/numeric/ublas/matrix.hpp&gt;
#include &lt;boost/numeric/ublas/matrix_proxy.hpp&gt;
#include &lt;boost/numeric/ublas/vector.hpp&gt;

int main()
{
    namespace ublas = boost::numeric::ublas;

    ublas::matrix&lt;double&gt; m{3, 4};

    m &lt;&lt;= 0, 1, 2, 3,
          3, 4, 5, 6,
          6, 7, 8, 9;

    double d = 0;

    for (size_t n = 1; n != m.size1(); ++n)
    {
        const ublas::matrix_row&lt;ublas::matrix&lt;double&gt;&gt; row1{m, 0};
        const ublas::matrix_row&lt;ublas::matrix&lt;double&gt;&gt; row2{m, n};

#if 1
        const auto e = row1 - row2; // creates an expression

        d += ublas::norm_2(e);      // uses the expression
#else
        const ublas::vector&lt;double&gt; v = row1 - row2;   // creates a vector (performance issue)

        d += ublas::norm_2(v);
#endif
    }

    return 0;
}
</code></pre>

<p>Does anyone know if this is allowed?</p>