Not the most optimised code, but it works.
With help of make_function
from this answer
template <typename ...Args>
std::function<void(Args...)> CombineTwoFunctionsHelper(std::function<void(Args...)> f, std::function<void(Args...)> g) {
return [f, g](Args ...args) {
f(args...);
g(args...);
};
}
template <typename F1, typename F2>
auto CombineTwoFunctions(F1 f1, F2 f2) -> decltype(make_function(f1)) {
return CombineTwoFunctionsHelper(make_function(f1), make_function(f2));
}
void print1(int i, std::string s) {
std::cout << "print1 " << i << s << std::endl;
}
void print2(int i, std::string s) {
std::cout << "print2 " << i << s << std::endl;
}
int main() {
auto fg = CombineTwoFunctions(print1, print2);
fg(1, "test");
}
Full code at Coliru
You should be able to improve it by adding (universal) references to argument and forward them to avoid copies. But note that you cannot move an argument twice.
As @0x499602D2 said in the comment, C++14 make it much eaiser
template <typename F1, typename F2>
auto CombineTwoFunctions(F1 f, F2 g) {
return [f, g](auto&& ...args) {
f(args...);
g(args...);
};
}
void print1(int i, std::string s) {
std::cout << "print1 " << i << s << std::endl;
}
void print2(int i, std::string s) {
std::cout << "print2 " << i << s << std::endl;
}
int main() {
auto fg = CombineTwoFunctions(print1, print2);
fg(1, "test");
}
Full code at Coliru