title: Array copy functor example: improvement/alternative?
body: <p><strong>Problem</strong>   </p>
  
  <p>Implement a copy functor 'interface' that works with both scalars and arrays.</p>
  
  <pre><code>CopyFunctor::operator()(T&amp; dest, const T&amp; src);
  </code></pre>
  
  <p>As an example, I want to generalize a sort function to work with both array of elements and array of 'inlined' arrays.</p>
  
  <pre><code>template &lt;typename T, typename CopyFunctor&gt; sort(T array[]);
  
  int array_of_elements[] = {7, 3, 5, 2};
  sort(array_of_elements);
  // after sorting: {2, 3, 5, 7}
  
  int array_of_arrays[] = {  3, 1,   2, 4,   7, 6,   5, 8};
  // inlined arrays are: {3, 1}, {2, 4}, {7, 6}, {5, 8}
  // these 'inlined' arrays are fixed-sized
  // after sorting using first element as key: { 2, 4,   3, 1,   5, 8,    7, 6}   
  // thus, the second element should be moved with the first element
  </code></pre>
  
  <p>Inside sort(T array[]), 
  to swap element (or group of elements) from position i to position j,
  we can call the functor in exactly the same manner regardless whether a is an array of elements or an array of 'inlined arrays':</p>
  
  <pre><code>template &lt;typename T, typename CopyFunctor&gt; sort(T array[], CopyFunctor copy) {
  
   // ...
  
     // swap a[i] and a[j]
     copy(b, a[i]);
     copy(a[i], a[j]);
     copy(a[j], b);
  
   // ...
  
   }
  </code></pre>
  
  <p>Here b is either a scalar or an array large enough to hold one 'inlined' array of within a.
  (For this example, I'd probably want a swap functor instead, but in my more complicated use case, I needed a copy functor.)</p>
  
  <p><strong>Assumptions</strong>  </p>
  
  <p>Once the array of inlined arrays is initialized, it will <em>never</em> be resized.</p>
  
  <p><strong>Example code</strong></p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;cstring&gt;
  using namespace std;
  
  template &lt;typename T&gt;
  struct ScalarSetter {
      void operator()(T&amp; a, const T&amp; b) const {
          a = b;
      }
  };
  
  template &lt;typename T&gt;
  struct ArraySetter {
      size_t _n;
      ArraySetter(size_t n) : _n(n) {}
      void operator()(T&amp; a, const T&amp; b) const {
          std::memcpy(&amp;a, &amp;b, sizeof(T)*_n);
      }
  };
  
  int main(int argc, char* argv[]) {
  
      {
        ScalarSetter&lt;float&gt; copy;
        float a = 1.0;
        float b = 2.0;
        copy(a, b);
        printf("a = %f\n", a);
        /* output:
         * a = 2.00000
         */
      }
  
      {
        size_t n = 3;
        ArraySetter&lt;float&gt; copy(n);
        float c[] = {.1, .2, .3, .4};
        float d[] = {.4, .5, .6, .7};
        copy(c[0], d[0]);
        for (size_t i = 0; i &lt; n+1; ++i) {
          printf("c[%d] = %f\n", i, c[i]);
        }
        /* output:
         * c[0] = 0.400000
         * c[1] = 0.500000
         * c[2] = 0.600000
         * c[3] = 0.400000
         */
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Since I am actually using Cuda/C++, I am more or less forced to use inlined arrays.</p>
  
  <p>The example code above compiles, runs, and passes valgrind memory test.
  But I feel a tad uncomfortable derving pointers to the input arguments in ArraySetter::operator()(...).</p>
  
  <p>(<em>Edit</em>: I think the code will break if an inlined array is not contiguous... Is there any guarantee on the contiguity of (dynamically) allocated arrays?)</p>
  
  <p>Is there another way of implmenting a consistent copy functor 'interface' that works with both scalars and arrays?</p>