0

Hi I got a variadic templates in C++

int sum(int a, int b) { return a + b; }

template<typename Func, typename... Args>
auto MainCall(Func func, Args&&... args)-> typename std::result_of<Func(Args...)>::type
{
    return func(std::forward<Args>(args)...);
}

template <typename... Args>
int call2(Args const&... args)
{
    return MainCall(sum, args...); /* calling template function MainCall,  
                                      how to pass the variable arguments to   
                                      Maincall from here?
                                   */
}

int _tmain(int argc, _TCHAR* argv[])
{
  cout << call2(4,5) << endl;
  return 0;
}

Am I doing a right way of passing arguments? I get the compiler error like this
error C3520: 'args' : parameter pack must be expanded in this context
see reference to function template instantiation 'int call2(const int &,const int &)' being compiled

What is the correct way of passing variable arguments to the MainCall template?

Note: Parameters need to be always variable so that MainCall can take any function with any number of arguments.. Just for an example I have writen sum function here..

7
  • What version of VisualStudio are you using? VS2013 Update 4, and gcc, both compile and run your code correctly. Unrelated to your error, but don't you want to perfectly forward the arguments to call2() also? Commented Dec 30, 2014 at 8:24
  • What you have here will compile and run once presented with the conveniently omitted #include list on clang. It will not compile for more, or less, than two arguments. What are you actually trying to accomplish? Commented Dec 30, 2014 at 8:24
  • @Praetorian:Yes you were right I have visual studio 2012 I tried to reinstall the C++ compiler strange now it works... I am trying to create template which takes name of the function as a string and arguments as its type..I have asked the question here link... I was just playing around with the templates to achieve that. Moreover the arguments does support only upto 5... passing more than 5 arguments it gives compiler error Commented Dec 30, 2014 at 8:49
  • 1
    I didn't even know VS2012 supported variadic templates. I thought its stdlib supported some faux varidiac template like behavior through the use of x-macros. Anyway, you might be able to increase that limit by defining VARIADIC_MAX (or something similar) to a larger number. Commented Dec 30, 2014 at 8:57
  • ` template<typename... funcname, typename... Args> int CallFunction(const std::string& funcname , Args&&... args) { if(!funcname.compare("sum")) { return MainCall(sum, args...); } else if(!funcname.compare("sum")) { return MainCall(size, args...); } else { return 0; } }` I am getting error error C2197: 'int (__cdecl *)(char *)' : too many arguments for call see reference to class template instantiation 'std::_Result_type<false,_Fty,_V0_t,_V0_t,_V2_t,_V2_t,_V4_t,_V4_t,_V6_t,_V6_t,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>' being compiled Commented Dec 30, 2014 at 14:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.