1

I have the following lines of code. I don't understand why template parameters need to be given (the highlighted part). Isn't the compiler able to deduce the type automatically? Since myfunc is defined already, I would think it is obvious which Combiner needs to be called.

#include <functional> #include <iostream> template <class T> void Combiner(double a, std::function<void (T)> func){ return func(std::to_string(a)); } void myfunc(std::string a){ std::cout << a << std::endl; return; } int main(){ Combiner***<std::string>***(12, myfunc); } 
2
  • If you just want a quick fix change it to void Combiner(double a, T func){. Still looking at the "why" part. Commented Jan 14, 2022 at 14:48
  • 1
    myfuncstd::function<void(std::string)>. Add this std::function<void(std::string)> fn_myfunc = myfunc; then Combiner(12, fn_myfunc); Commented Jan 14, 2022 at 14:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.