title: How to make a function wrapper that can wrap any function?
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  template&lt;typename Func&gt;
  class FuncWrapper {
      Func func;
      std::string name;
  
  public:
  
      FuncWrapper(Func func, std::string name) : func(func), name(std::move(name)) {}
  
      template&lt;typename ...Args&gt;
      auto operator()(Args ...args) {
          std::cout &lt;&lt; "Entering " + name + '\n';
          auto ret = func(args...);
          std::cout &lt;&lt; "Exiting " + name + "\n\n";
          return ret;
      }
  };
  
  template&lt;typename Func&gt;
  auto makeFuncWrapper(Func func, std::string name) {
      return FuncWrapper&lt;Func&gt;(func, name);
  }
  
  int main() {
  
      auto helloWorld = []() { std::cout &lt;&lt; "Hello World\n"; };
      auto addTwoNums = [](int a, int b) { std::cout &lt;&lt; "Adding numbers...\n"; return a + b; };
  
      // makeFuncWrapper(helloWorld, "helloWorld")(); // error: 'void ret' has incomplete type.
      std::cout &lt;&lt; makeFuncWrapper(addTwoNums, "addTwoNums")(4, 5) &lt;&lt; '\n';
  
  }
  </code></pre>
  
  <p>This class <code>FuncWrapper</code> works fine and adds an additional functionality to the passed function, until the passed function is a function that returns <code>void</code>. I get an error that the type of <code>ret</code> is incomplete. Is there any other way to make it work even with functions with return type of <code>void</code>? I know that I can't have a <code>void</code> variable, but here, the only purpose of <code>ret</code> is to be returned after the function is done and it's not gonna be used in a wrong way. Is there a solution or some workaround to make it work? Any better way of implementing a function wrapper that wraps any function?</p>