与函数调用混淆

rim*_*ire -2 c++ function-call

为什么这个函数调用有效?该函数func(int,int)被声明为采用整数,但即使用double调用它也是有效的.为什么会这样?

#include<iostream>
using namespace std;


void func(int a,int b){


cout<<"a is "<<a;
cout<<"\nb is "<<b;

}

int main(){
func(12.3,34.3);


}
Run Code Online (Sandbox Code Playgroud)

Gar*_*rez 6

它隐含地转换doublesints.您会注意到func(12.3,34.3);它们将它们打印为12和34.当您调用函数时,编译器会查找具有最佳匹配签名的函数.在这种情况下,它找到void func(int, int)并调用它,因为它可以进行隐式转换.