0

I tried overloading a function with boost::function with different signatures, it did not work. I tried using template<Signature> Connection *connect(boost::function<Signature> f) which also failed because boost::bind doesn't implicitly convert that way

What I'm trying to do is exactly what boost::signals does. Boost signals can accept both, function object without arguments and function objects with N arguments

I want to know how I can accept both type of function objects in one function.

Saif Ur Rehman
  • 338
  • 3
  • 14

1 Answers1

1

You can use variadic templates:

template<typename R, typename... Args>
Connection* connect(boost::function<R (Args...)> f);

This will accept both a function that takes 0 arguments or one that accepts N arguments.

David G
  • 94,763
  • 41
  • 167
  • 253
  • I tried that, however, I got a compile error "ServerSignal(70): error C2143: syntax error : missing ',' before '...'". Maybe my compiler is not up to date? I am using Visual Studio 2012. Is there any other way? I would like to use a more backward compatible method. Any idea how boost does it? I am using 2 function objects, one with arguments and other without. I need to use the correct connect method to decide which function object to use. – Saif Ur Rehman Jan 08 '14 at 02:45
  • @SaifUrRehman Visual Studio 2012 doesn't support variadic templates unfortunately, and I'm not sure how boost does it. My only suggestion would be to use function overloading on `function` and `function`. – David G Jan 08 '14 at 02:56
  • @SaifUrRehman I think the answer to this question might give you some help. http://stackoverflow.com/questions/18237350/using-variadic-macros-or-templates-to-implement-a-set-of-functions – David G Jan 08 '14 at 03:23
  • @SaifUrRehman Another thing to do would be to implement a type list which is relatively simple. Here is the source code to the type lists shown in Modern C++ Design -- http://loki-lib.sourceforge.net/html/a00681.html – David G Jan 17 '14 at 16:29