C++ 传递const int*和int时会调用重载函数的不同版本(const int*const&;/&;)*
我在代码中遇到了一个错误,调用了错误的重载函数 问题归结为:C++ 传递const int*和int时会调用重载函数的不同版本(const int*const&;/&;)*,c++,pass-by-reference,implicit-conversion,rvalue,lvalue,C++,Pass By Reference,Implicit Conversion,Rvalue,Lvalue,我在代码中遇到了一个错误,调用了错误的重载函数 问题归结为: void func(const int* const& ptr){ std::cout << "LValue reference\n"; } void func(const int* const&& ptr){ std::cout << "RValue reference\n"; } int main(){ const int* ptr; func(p
void func(const int* const& ptr){
std::cout << "LValue reference\n";
}
void func(const int* const&& ptr){
std::cout << "RValue reference\n";
}
int main(){
const int* ptr;
func(ptr);
return 0;
}
void函数(常量int*const&ptr){
std::cout一个int*
与一个const int*
不是同一类型。因为它们不是同一类型,所以必须进行转换,因为您不能将引用绑定到不同的类型,然后声明引用它(处理派生对象时除外)
这意味着int*
用于创建一个临时const int*
,该临时指针是一个右值。由于它是一个右值,因此将选择右值引用重载