1

Given a variadic macro of the form:

#define MY_CALL_RETURN_F(FType, FId, ...) \
  if(/*prelude omitted*/) {             \
    FType f = (FType)GetFuncFomId(FId); \
    if(f) {                             \
      return f(__VA_ARGS__);            \
    } else {                            \
      throw invalid_function_id(FId);   \
    }                                   \
  }                                     \
/**/

-- how can this be rewritten to a variadic function template?

template<typename FType, typename ...Args>
/*return type?*/ tmpl_call_return_f(MyFunId const& FId, /*what goes here?*/)
{
  ...
  FType f = (FType)GetFuncFomId(FId);
  return f(/*what goes here?*/);
  ...
}

Update: I'm specifically interested in how to declare the reference type for the Args: && or const& or what?

Update: Note that FType is supposed to be a "plain" function pointer.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Martin Ba
  • 37,187
  • 33
  • 183
  • 337

2 Answers2

2

It would look something like this:

template<typename FType, typename ...Args>
std::result_of<FType(Args...)>::type tmpl_call_return_f(MyFunId const& FId, Args... &&args)
{
  FType f = (FType)GetFuncFomId(FId)
  return f(std::forward<Args>(args)...);
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
1

If you use std::function as FType, then this should work:

template <typename FType, typename ...Args>
typename FType::result_type tmpl_call_return_f(MyFunId const& FId, Args... args) {
  // ...
  return f(args...);
}
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • How is `FType` going to be deduced? Or are you planning on specifying it explicitly? If so, you might as well just specify the return type... – Kerrek SB Aug 23 '11 at 13:48