Answer the question
In order to leave comments, you need to log in
Passing a function - templates or std::function?
I have a function that takes another function as one of its arguments - and both a lambda and a regular function can be passed.
I don't want to use function pointers, because they don't look good with lambdas and C++14 goodies.
Which is better to use -
template<typename Func> void MyFunc (const Func & otherFunc);
void MyFunc (const auto & otherFunc);
void MyFunc (const std::function<void()> & otherFunc);
void MyFunc (const void (&otherFunc)());
? void Subcalc(
std::string &line,
const char &symbol,
const std::function<std::string(const std::string&, const std::string&)>
&mathFunc);
std::string s = " 5+5*3+1 ";
Subcalc ( s, '*', myMathFunc); // s == "5+15+1"
std::string s = " 5+5*3-1 ";
Subcalc ( s, '*', myStringFunc); // s == "5+555+1"
void Subcalc(
std::string &line,
const char &symbol,
const std::function<std::string(const std::string&, const std::string&)>
&mathFunc);
void Subcalc(
std::string &line, const char &symbol, const auto &mathFunc);
template<typename Func>
void Subcalc(
std::string &line,
const char &symbol,
const Func &mathFunc);
void Subcalc(
std::string &line,
const char &symbol,
std::string (&mathFunc)(const std::string&, const std::string&));
Answer the question
In order to leave comments, you need to log in
First option:
Ensures that otherFunc is inlined into MyFunc, which is the preferred option where maximum performance is required. But template functions cannot be virtual. That is, this option is very well suited for utility functions, and is poorly suited for public API methods and at module boundaries, since inline is not needed there, but virtuality and implementation hiding are needed.
Important note! The above code is not entirely correct, in such cases always use the transfer by universal reference:
This option covers both const functors and those that require mutability to work. If you plan to keep the functor, this also allows you to use perfect forwarding - in fact, the only way that works correctly with any functor passed:
Second option:
Can be used if you do not need a functor type. In fact, it is equivalent to the previous one.
Third option:
Take the negation of my previous arguments and apply here. Can be used with virtual functions, more suitable for API, but loses performance. Namely, calling otherFunc is equivalent to calling a virtual function.
Fourth option:
Works only with simple, globally defined functions. You should almost always use std::function instead.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question