3

I have similar case but more complex. I am trying to call a template function inside a normal function but I can't compile...

#include <iostream>

using namespace std;

template<class T>
void ioo(T& x) { std::cout << x << "\n"; }

template<class T, class ReadFunc>
void f(T&& param, ReadFunc func) {
    func(param);
}

int main() {
    int x = 1;
    std::string y = "something";
    f(x, &::ioo);
}

1 Answer 1

5

ioo is a function template, not a function, so you can't take its address.

This would however work since it instantiates the function void ioo<int>(int&):

f(x, &ioo<decltype(x)>);

and as noted by Jarod42 in the comments, you could make it into a lambda:

f(x, [](auto& arg){ioo(arg);});
Sign up to request clarification or add additional context in comments.

3 Comments

@Jarod42 Indeed - added that as an option. Thanks!
Thanks that second comment was helpful for me. I had multiple params but I use f(x, [](auto&&... arg){ioo(...arg);});
@IonutAlexandru You are welcome! You may want to take a look at std::forward too in order to do perfect forwarding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.