发布网友 发布时间:2022-03-31 05:51
共1个回答
热心网友 时间:2022-03-31 07:21
如果两个或两个以上的重载接受不同的指针类型,过载std::nullptr_t是需要接受一个空指针参数.
#include <cstddef>
#include <iostream>
void f(int* pi)
{
std::cout<<Pointer to integer overload\n;
}
void f(double* pd)
{
std::cout<<Pointer to double overload\n;
}
void f(std::nullptr_tnullp)
{
std::cout<<null pointer overload\n;
}
int main(){
int* pi;
double* pd;
f(pi);
f(pd);
f(nullptr);// would be ambiguous without void f(nullptr_t)// f(NULL); // ambiguous overload: all three functions are candidates
}
Output:
Pointer to integer overload
Pointer to double overload
null pointer overload