Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function overloading with ellipsis

Tags:

c++

Can i actually use a function overloading like this:

#include <iostream>

void foo(...)
{
   std::cout << "::foo(...) \n";
}

void foo(int)
{
   std::cout << "::foo(int) \n";
}

int main()
{
   foo(0);
   foo('A');
   foo("str");
   foo(0, 1);
}

What standard says about it? And in what kind of situations i'll get ::foo(...)?

like image 863
FrozenHeart Avatar asked Sep 15 '12 10:09

FrozenHeart


People also ask

What does ellipsis mean in C++?

Ellipsis in C++ allows the function to accept an indeterminate number of arguments. It is also known as the variable argument list. Ellipsis tells the compiler to not check the type and number of parameters the function should accept which allows the user to pass the variable argument list.

How do you fix an ambiguous call to overloaded function?

There are two ways to resolve this ambiguity: Typecast char to float. Remove either one of the ambiguity generating functions float or double and add overloaded function with an int type parameter.

What is function overloading explain with example?

Function Overloading in C++When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.

What is ambiguity in function overloading?

When the compiler is unable to decide which function it should invoke first among the overloaded functions, this situation is known as function overloading ambiguity. The compiler does not run the program if it shows ambiguity error.


1 Answers

void foo(int)

will accept one argument of type int.

void foo(...)

accepts any number of arguments, of any type. It will be selected when the call doesn't have a single int argument. Not very useful, in general.

Also note that it is undefined behavior to pass objects of class type to ....

like image 121
Bo Persson Avatar answered Oct 25 '22 07:10

Bo Persson