How to make template function call less verbose

There is a function

template <class ...T>
void foo(std::function<void(T...)> callback);

to which I pass the callback.

I would like to do something like

foo(bar);

where baris for example

void bar(int a, long b, double c, float d);

but it gives me

error: no matching function for call to bar(void (&)(int, long int, double, float))

I need to call foohow

foo(std::function<void(int, long, double, float)>(bar));

which is too verbose. Even

foo<int, long, double, float>(bar);

would be better.

foo(bar);

will be just perfect.

In any case, how can I make the calls fooless detailed?

Announcement

Edit: foo should remain unchanged.

+4
source share
3 answers

I would write a wrapper function that converts a function pointer into a wrapper std::function:

template <typename... T>
void foo(std::function<void (T...)> f) {}

template <typename... T>
void foo(void (*f)(T...)) {
    foo(std::function<void (T...)>(f));
}

foo() you can then call in any case:

void bar(int,double) {}

void foo_caller() {
    foo(std::function<void (int,double)>(bar));
    foo(bar);
}

: -

-- - :

template <typename C,typename... T>
void foo(void (C::*f)(T...)) {
    foo(std::function<void (C *,T...)>(f));
}

this -. :

struct quux {
    void mf(char *,double) {}
};

void foo_caller() {
    foo(&quux::mf);
}
+8

foo ,

#include <functional>

template <class Ret, class ...T>
void foo(Ret callback(T... params))
{
}

void bar(int a, long b, double c, float d){}

int main() 
{
    foo(bar);
}
+1

, foo, ++ 11 , foo :

template <class ...T>
void foo(void(*callback)(T...)) {
   // .....
}

lambdas,

template <class LambdaType>
void foo(LambdaType callback) {
   // .....
}

, -, , , foo.


, T... - int, long, double, float, , .

, void(int, double) - MyTempalte<T...>, , T... int, double, , MyTemplate . , MyTemplate - ?

, , std::function .

+1

All Articles