Function overloading with ellipsis

Can I use the overload function as follows:

#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 talks about this? And in what situations will I get :: foo (...)?

+5
source share
4 answers
void foo(int)

will take one type argument int.

void foo(...)

accepts any number of arguments of any type. It will be selected if there are no arguments in the call int. Overall not very helpful.

Also note that this undefined behavior passes objects of type class to ....

+2
source

In N3337 I see: -

13.3.2
-, m , , (8.3.5). , , " (13.3.3.1.3).

+2

:

  void foo (...) 

foo .

, , .

, :

foo(//Some single int).

:

foo(0)     //Calls foo(int).
foo('A)    //Calls foo (int). as you can convert a char to an int.
foo("str") //Calls foo(...) . as you can not convert a string to an int.
foo(1,2)   //Calls foo(...) . as this is the only possible function 
             cause the second foo function only takes one int.
0

void foo(...) . , .

foo(0);             //This will call void foo(int) function
foo('A');           //This will call void foo(int) function
foo("str");         //This will call void foo(...) function
foo(0, 1);          //This will call void foo(...) function

:

Although ellipsis works great when overloading a function, it is not recommended to use variable functions. At least until you have significantly more experience in C ++ to understand the pitfalls. I would suggest using it only with the catch try block, where there are situations where the error cannot be predicted.

0
source

All Articles