1

Each one takes an std::function as the first parameter,

I am not being able to call the variadic template to replace them all, any help would be appreiciated.

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

template <typename ... FArgs, typename ... Args>
void run_callback(std::function<void(FArgs...)> && func, Args && ... as)
{
    std::cout << "vardiac called\n" ;
}
void run_callback(std::function<void()>&& func) {
    func();
}

void run_callback(std::function<void(int)>&& func, int data)
{
    func(data);
}

void run_callback(std::function<void(int, int)>&& func, int data1, int data2) {
    func(data1, data2);
}
void sum(int , int)
{
    
}
int main()
{
    run_callback([](){std::cout << "none" << '\n';});
    run_callback([](int j){std::cout << "none1: " << j <<  '\n';},12);
    run_callback([](int j, int i){std::cout << "none2: " << j << ',' << i << '\n';},12,17);
    run_callback(sum, 5,4);
}
Libra
  • 33
  • 4

1 Answers1

0
template <typename F, typename ... Args>
void run_callback(F&& func, Args && ... as)
{
    func(std::forward<Args>(as)...);
}

Demo

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • I am aware of this .. but the signature is different – Libra Feb 28 '23 at 07:16
  • if you look at this answer, https://codereview.stackexchange.com/questions/190603/generic-message-system-using-variadic-templates/190612#190612, he used the same signature that I am trying to make it work, my question is can it be called in this specifi way or not? – Libra Feb 28 '23 at 08:52
  • `FArgs` could only be deduced from `std::function` if you pass an argument of type `std::function`, not an arbitrary callable. For one thing, `std::function` has a constructor that takes pretty much anything; e.g. `std::function` would take `int f(long)`. For another, a lambda itself could be variadic and/or generic, taking arbitrary number and types of arguments. The compiler doesn't consider user-defined conversions when performing template parameter deduction. So no, your original signature has no hope of working. – Igor Tandetnik Feb 28 '23 at 14:51
  • I wonder if the author of that answer actually tried to compile their code. I'm reasonably sure it won't compile. – Igor Tandetnik Feb 28 '23 at 14:55
  • in this answer as well, https://stackoverflow.com/a/48967736/21299064 – Libra Feb 28 '23 at 14:59
  • That answer plays a trick: it uses the same parameter pack both for `std::function` and for the following arguments. `Args` is deduced from those following arguments. That can work. (The second formulation that uses two packs likely would not work, for the same reasons yours doesn't.) I'm not sure why you would want that though, rather than just taking the callable directly. Why do you insist on taking `std::function` as a parameter? With all due respect, this sounds like an [XY problem](https://xyproblem.info/) – Igor Tandetnik Feb 28 '23 at 15:01
  • just POC, I saw the code and didn't work for me, I knew before hand about your approache – Libra Feb 28 '23 at 15:05
  • Well, this approach has the small additional benefit of, you know, actually compiling and working. – Igor Tandetnik Feb 28 '23 at 15:07
  • yes it does, and you can use std::bind to make it callable, for downstream processing – Libra Feb 28 '23 at 15:11
  • Or a lambda: `auto callable = [=]() { func(as...); };` – Igor Tandetnik Feb 28 '23 at 15:15