Overriding Functions with variable argument list.
-
I had a understanding that overriding the the function with variable argument list will result in to ambiguity. However when i tried to compile and run following code on VC++ 6.0, its working as expected : ///////////////////////////////////// void Func1( int i, ... ) //Lets refer it as Func1VariableArg { // do something. } void Func1( int i, int j) //Lets refer it as Func1FixedArg { // Do something. } class A { }; int main(int argc, char* argv[]) { Func1(1, 1); // Func1FixedArg is called as expected. Func1(1, "1"); // Func1VariableArg is called as expected. A a; Func1(1, a); // Func1VariableArg is called as expected. // This is the spoil spot Func1(1, 1.0)// here we would expect Func1VariableArg to //be called as second argument is double, however compiler // implicitely typecasts it to int and calls the Func1FixedArg. // however a warning for the same is issued at complie time. return 0; } ///////////////////////////////////// Some one has any comments about this practice. Do we have any issues in this practice ?, any comments from C++ language specifications on this. Thanks - Suyash
-
I had a understanding that overriding the the function with variable argument list will result in to ambiguity. However when i tried to compile and run following code on VC++ 6.0, its working as expected : ///////////////////////////////////// void Func1( int i, ... ) //Lets refer it as Func1VariableArg { // do something. } void Func1( int i, int j) //Lets refer it as Func1FixedArg { // Do something. } class A { }; int main(int argc, char* argv[]) { Func1(1, 1); // Func1FixedArg is called as expected. Func1(1, "1"); // Func1VariableArg is called as expected. A a; Func1(1, a); // Func1VariableArg is called as expected. // This is the spoil spot Func1(1, 1.0)// here we would expect Func1VariableArg to //be called as second argument is double, however compiler // implicitely typecasts it to int and calls the Func1FixedArg. // however a warning for the same is issued at complie time. return 0; } ///////////////////////////////////// Some one has any comments about this practice. Do we have any issues in this practice ?, any comments from C++ language specifications on this. Thanks - Suyash
if the compiler can find a function which signature fits exactly to the argument types it receives, then there's no problem. the ambiguity can arrise when the compiler has to do some implicit conversions... in you case
Func1(1, 1);
results in callingvoid Func1(int, int)
because literal numbers are firstly interpreted asint
s. i don't know the priorities of the compiler's implicit casts, but take this for instance :Func1(1, 'a');
'a' is of typechar
, but no function Func1(int, char) exists, so it continues looking if this call fits another existing overload before casting some parameters. only if the compiler makes some implicit casts an ambiguity can come, because the call can then fit several overloads...