title: Abstract away nested loops in C++
body: <p>I am relatively new to C++ and am building a matrix class for carrying out with it common linear algebra operations. Some snippet of the <code>hpp</code>:</p>
  <pre><code>template&lt;const int rows, const int columns, typename T=float&gt;
  struct Matrix3D
  {
      const int shape[2] = {rows, columns};
      
      float content[rows][columns]{};
      
      Matrix3D()
      {
          for (int i = 0; i &lt; rows; i++)
          {
              for (int j = 0; j &lt; columns; j++)
              {
                  this-&gt;content[i][j] = 0.;
              }
          }
      }
      
      explicit Matrix3D(const T (&amp;arr)[rows][columns])
      {
          for (int i = 0; i &lt; rows; i++)
          {
              for (int j = 0; j &lt; columns; j++)
              {
                  this-&gt;content[i][j] = arr[i][j];
              }
          }
      }
      
      bool operator==( const Matrix3D&lt;rows, columns&gt; &amp;mat ) const noexcept
      {
          int equal = 0;
          
          for (int i = 0; i &lt; rows; i++)
          {
              for (int j = 0; j &lt; columns; j++)
              {
                  equal += this-&gt;content[i][j] == mat.content[i][j];
              }
          }
          return  equal == rows * columns;
      }
      
      bool operator!=( const Matrix3D&lt;rows, columns&gt; &amp;mat ) const noexcept
      {
          int equal = 0;
          
          for (int i = 0; i &lt; rows; i++)
          {
              for (int j = 0; j &lt; columns; j++)
              {
                  equal += this-&gt;content[i][j] == mat.content[i][j];
              }
          }
          return  equal != rows * columns;
      }
  
      ...
  
  };
  </code></pre>
  <p>You get the idea: for pretty much all the operations that I will need to manipulate that matrix object I am going to need these double loops in every method.</p>
  <p>Is there a way to abstract that, for example, to a <code>static</code> method, and pass the core looping expression as argument? It is worth noting that my solution must be compatible with <code>CUDA</code>, meaning I cannot use <code>std::vector</code>, or <code>Eigen</code>, or similar.</p>
  <p>Thanks in advance.</p>