user12716429
user12716429

Reputation: 3

Issue with overloaded variadic function in c++

void fun(int a, ....)
{
  cout<< "variadic function"<<endl;
}
void fun(int a, int b)
{
  cout<< "function"<<endl;
}
int main()
{ 
  fun(1,2);
}

output : function

I have overloaded the variadic function. How do I make sure it should link to the variadic function?

Upvotes: 0

Views: 147

Answers (1)

TonySalimi
TonySalimi

Reputation: 8427

Keep in mind that according to the standard (see here):

Because variadic parameters have the lowest rank for the purpose of overload resolution, they are commonly used as the catch-all fallbacks in SFINAE.

functions with variadic parameters always have the lowest priority for overload resolution. As a result, in your case, the second overload, (i.e. fun(int a, int b)) will be called. But as already advised by other users in the comments, try to never use functions with variadic parameters in C++ due to the lack of type checking of the arguments.

Upvotes: 1

Related Questions