0

I'm trying to accept multiple arguments in a std::function, but I got errors:

#include <functional>

template <typename... Args>
void caller(std::function<void(Args&&...)> function)
{

}

int main()
{
    caller([&] () { });
}

The error is:

main.cpp:11:22: error: no matching function for call to 'caller(main()::<lambda()>)'
     caller([&] () { });
                      ^
main.cpp:11:22: note: candidate is:
main.cpp:4:6: note: template<class ... Args> void caller(std::function<void(Args&& ...)>)
 void caller(std::function<void(Args&&...)> function)
      ^
main.cpp:4:6: note:   template argument deduction/substitution failed:
main.cpp:11:22: note:   'main()::<lambda()>' is not derived from 'std::function<void(Args&& ...)>'
     caller([&] () { });

How can I make this work?

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
yayuj
  • 2,194
  • 3
  • 17
  • 29

1 Answers1

-2

Maybe

#include <functional>
#include <iostream>

template<typename F>
void caller(std::function<F> function)
{

}

void f() {
    caller<void()>([&] () {
        std::cout << "Hi." << std::endl;
    });
}

or even

#include <functional>
#include <iostream>

template<typename F>
void caller(F function)
{

}

void f() {
    caller<void()>([&] () {
        std::cout << "Hi." << std::endl;
    });
}

Both codes do compile, but I don't know how can they be useful to youm, if I don't know what you to do...

lvella
  • 12,754
  • 11
  • 54
  • 106