1

I want to pass std::function as a callback parameter to another function. The function to be bound is a static function with variable argument list.

The static function is as follows:

void abc::static_func(const char* const text,...);

abc is a static class and static_func is a static function.

The function where std::function parameter to be received:

void xyz(std::function<void(const CHAR* const text, ...)> arg);

I tried following:

std::function<void(const CHAR* const text, ...)> callback = std::bind(void(*)(const CHAR* const text, ...))(&abc::func);

I do not want to add auto as callback has to be passed to xyz function further.

I have looked up over online and I got a variadic template related answers but they do not fit into the above problem and mostly auto is used in all the answers.

Any suggestions/help would be much appreciated.

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
  • 2
    Is `...` in your code a C vararg list? – Piotr Skotnicki Jun 14 '20 at 06:36
  • If so, then just use a pointer to function. – Piotr Skotnicki Jun 14 '20 at 06:55
  • @PiotrSkotnicki - I am using it in a cpp file. Are you suggesting me to use a function pointer instead of C++ std::function? There is no way to use std::function for a variable arg list? – madhura hegde Jun 14 '20 at 07:49
  • @t.niese - I looked into the answer, and I did not find how binding works for variable args. It is more specific to prinft and vprintf. – madhura hegde Jun 14 '20 at 07:49
  • 1
    There's no any binding in your example. – Piotr Skotnicki Jun 14 '20 at 07:50
  • 1
    Which arguments are you trying to bind ? – Piotr Skotnicki Jun 14 '20 at 07:57
  • @madhurahegde the linked question, uses `prinft` as example, but it applies to all C vararg list. `std::function` is not valid it has to be `std::function`, (as described in the duplicate) and if you want to call that `std::function` with a variable number of arguments you would do that as described in the duplicate. Direct binding of arguments is not possible, you need to use indirection. – t.niese Jun 14 '20 at 08:19
  • @t.niese Thank you, it worked. When you emphasized that binding is not possible, it clicked me :) – madhura hegde Jun 14 '20 at 13:05

0 Answers0