'C++ Passing templated functions to function

I need to pass a templated function to a function.

Until now I haven't found any great advices on google. This is what I tried:

#include <iostream>
#include <sstream>
using namespace std;

struct event {
  bool signal;
};

template<typename T>
void out_stream(T &stream, event &evt) {
  stream << evt.signal;
}

template <typename F>
void doOperation(F f)
{
  event test;
  test.signal = 0;
  stringstream ss;

  f(ss, test);
}

int main() {

  doOperation(out_stream);

  return 0;
}

And that's the error the compiler gives me:

main.cc:27:3: error: no matching function for call to 'doOperation'
  doOperation(out_stream);
  ^~~~~~~~~~~
main.cc:16:6: note: candidate template ignored: couldn't infer template argument 'F'
void doOperation(F f)
     ^
1 error generated.

Some (I hope) useful information about my g++ compiler setup:

  • Apple clang version 13.0.0 (clang-1300.0.29.30)
  • Target: x86_64-apple-darwin20.6.0
  • Thread model: posix

Thank you in advance :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source