0

I have refered this link to meet my requirement . But when I am trying to implement the same in my code, it is throwing an error.

template<typename T, typename... Args>
void fun(T t, Args... args)
{
    cout << t;
}
int main()
{
    fun(1, 2.0, "Ranjan", "hi");//Error happens here
return 0;
}

The error at fun() is template<class T, <error type>>

What is going wrong here?

Community
  • 1
  • 1
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122

2 Answers2

3

VS2010 does not support variadic templates. See C++11 Features. VS2012 does not support it either, according to that page, so upgrading is not a solution at the moment.

Search for c++03 mimic variadic templates to determine if there is an alternative (one example from this site: How to implement "Variadic Template" with pre-c++0x(VS2008)?).

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
1

The problem is that you are using only the first, and not other template arguments. The g++ warning clearly explains it.

This example uses all arguments, and add a function for no arguments :

#include <iostream>

void fun()
{
    std::cout<<std::endl;
}
template<typename T, typename... Args>
void fun(T t, Args... args)
{
    std::cout << t;
    fun(args...);
}
int main()
{
    fun(1, 2.0, "Ranjan", "hi");//Error happens here
}
BЈовић
  • 62,405
  • 41
  • 173
  • 273