title: Template function pointer parameter vs constructor argument
body: <p>I have a dilemma about how  I should pass a function pointer that will be used extensively by a class throughout the object lifetime. I have  thought of 2 solutions:  </p>
  
  <ul>
  <li>Passing the function pointer to the constructor and storing it inside the class:  </li>
  </ul>
  
  <pre><code>    using Func = int(*)(int);
  
      class A
      {
      public:
          explicit A(int n, Func f) 
              : _f(f), _n(n)
          {}
  
          int Process()
          {
              return f(n);
          }
  
      private:
          Func _f;
          int _n;
      };
  </code></pre>
  
  <ul>
  <li>Or using a template parameter:  </li>
  </ul>
  
  <pre><code>    using Func = int(*)(int);
  
      template&lt;Func f&gt;
      class A
      {
      public:
          explicit A(int n) 
              : _n(n)
          {}
  
          int Process()
          {
              return f(n);
          }
  
      private:
          int _n;
      };
  </code></pre>
  
  <p>I think the template solution is more elegant but I am not really sure if it is the best solution.
  And as a subsidiary question in the template solution, if only the Process method is using the template parameter can I still put the constructor in a source file and keep the Process method in the header file?</p>