title: Passing functions as arguments in C++
body: <p>I'm a bit rusty in C++ and I'm switching after a year of python. Naturally I would like to translate the laziness coming from python to C++.</p>
  
  <p>I just discovered rot13 and I'm all excited about it. I found 3 ways of doing it and I wanted to do a little performance test.
  I wanted to see also if there is a difference in modifying the string in place or creating a new one. So I ended up having 6 functions.</p>
  
  <p>In the first method, I use a std::map to map the characters, thus I've built a class that initializes the map, in the second I use a ternary operator, and the third I use a bit shift.</p>
  
  <p>Now the functions prototypes look like this</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// map dependent
  void Rot13::convert_inplace(string&amp; mystr){
  
  string Rot13::convert(const string&amp; mystr){
  
  // ternary operator
  void bitrot_inplace(string&amp; mystr){
  
  string bitrot(const string&amp;  mystr){
  
  // bit shift
  void bitshiftrot_inplace(string&amp; mystr){
  
  string bitshiftrot(const string&amp; mystr){
  </code></pre>
  
  <p>I wanted to construct a function that accept those functions as arguments to then calculate the time and print the results</p>
  
  <p>So I had a look at stackoverflow, <a href="https://stackoverflow.com/questions/130322/how-do-you-pass-a-member-function-pointer">1</a>, <a href="https://stackoverflow.com/questions/130322/how-do-you-pass-a-member-function-pointer">2</a>, and I came up with this</p>
  
  <pre class="lang-cpp prettyprint-override"><code>typedef void (*vfc)(string str);
  
  void printtime_inplace(string title, vfc func){
  </code></pre>
  
  <p>I tried this construction yet this means I'm limited by the <code>vfc</code> return type which in my case is either <code>void</code> or <code>string</code>, and by the fact I need to pass the pointer of the class.
  Thus I will have to do 3 functions to accommodate the different functions, namely a function for the class member function, a function for the void return type and a function for the string return type.</p>
  
  <p>So I asked myself, is this the case where I really need to use templates to not write 3 times the same function? I'm really not confident with templates but should I do 3 <code>typedefs</code> and structure the printtime function to accept a template? Moreover is there a way to tell the template you will accept only these types (namely the one I defined)?</p>
  
  <p>An other question, is this let's say a good design? or would you suggest an other design? An other implementation?</p>