我真的可以像这样使用函数重载:
#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);
}
Run Code Online (Sandbox Code Playgroud)
标准说的是什么?在什么样的情况下我会得到:: foo(...)?
当您通过以下方式声明函数时:
void foo (...)
Run Code Online (Sandbox Code Playgroud)
这意味着 foo 接受任意数量的参数。
因此,当这是必须合适的函数时,就会调用该函数。
在你的情况下,只要你不写:
foo(//Some single int).
Run Code Online (Sandbox Code Playgroud)
在您的特定主体中,这将会发生:
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.
Run Code Online (Sandbox Code Playgroud)