C ++ - Metaprogrammierung mit Vorlagen versus Inlining

Lohnt es sich, Code wie den folgenden zu schreiben, um Array-Elemente zu kopieren:

#include <iostream>
using namespace std;


template<int START, int N> 
struct Repeat { 
  static void copy (int * x, int * y) {
   x[START+N-1] = y[START+N-1];
   Repeat<START, N-1>::copy(x,y);
  }
};

template<int START> 
struct Repeat<START, 0> { 
  static void copy (int * x, int * y) {
   x[START] = y[START];
  }
};



int main () {


   int a[10];
   int b[10];

             // initialize
   for (int i=0; i<=9; i++) {
     b[i] = 113 + i;
     a[i] = 0;
   }

            // do the copy (starting at 2, 4 elements)
  Repeat<2,4>::copy(a,b);

             // show
   for (int i=0; i<=9; i++) {
   cout << a[i] << endl;
   }

} // () 

Oder ist es besser, eine Inline-Funktion zu verwenden?

Ein erster Nachteil ist, dass Sie keine Variablen in der Vorlage verwenden können.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage