Take the 2-minute tour ×

I want to write a function that you call like so:

f("a", 1,                 [](float a                  ) { … });
f("a", 1, "b", 2,         [](float a, float b         ) { … });
f("a", 1, "b", 2, "c", 3, [](float a, float b, float c) { … });

What the function does is immaterial. The point is that each pair of char*/integer parameters to f corresponds to a float parameter to the lambda function. I get as far as this:

template <typename ...Args>
void f(Args... args, std::function<void(???)> cb);

How can I declare the functor type so that the cardinality of its parameters matches the cardinality of the input pairs?

share|improve this question
Do you need the template to be that specific so other overloads of f can be selected when the parameters aren't those types? If yes, you need a SFINAE technique. If no, I'd recommend making the template more general, and add a static_assert in the template definition to make it easier to debug incorrect calls. –  aschepler Aug 12 '12 at 7:46
Why not just use something like template<typename... Args, typename T> void f(Args&&... args, T func)? –  KennyTM Aug 12 '12 at 7:49
@aschepler: The function will only be overloaded for pairs of char*/int. When you say "SFINAE" do you mean something like KennyTM's suggestion? –  Marcelo Cantos Aug 12 '12 at 8:19
@KennyTM: I would like to be able to stipulate the functor's signature, but your suggestion will suffice. If you make it an answer, I'll probably accept it. –  Marcelo Cantos Aug 12 '12 at 8:21
add comment

1 Answer

up vote 1 down vote accepted

When requiring a function object argument, typically you should just allow the function to accept any type, and let the compiler emit the errors automatically when the type actually mismatch.

template <typename... Args, typename F>
void f(const F& func, Args&&... args) { ... }

(Note that I have moved the function argument to the front, because the variadic part will consume all arguments otherwise.)

share|improve this answer
add comment

Your Answer


