std :: nullptr_t类型的用法是什么? [英] What are the uses of the type `std::nullptr_t`?

查看:530
本文介绍了std :: nullptr_t类型的用法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学到了 nullptr ,除了可以转换为任何指针类型(但不是任何整数类型)也有自己的类型 std :: nullptr_t 。所以有可能有一个方法重载接受 std :: nullptr_t



如果多个重载接受指针类型,则 std :: nullptr_t的重载需要

解决方案

必须接受 nullptr 参数。如果没有 std :: nullptr_t 重载,那么在传递 nullptr 时应该选择哪个指针重载。 p>

示例:

  void f(int * intp)
{
//传递一个int指针
}

void f(char * charp)
{
//传递一个字符指针
}

void f(std :: nullptr_t nullp)
{
//传递空指针
}


I learned that nullptr, in addition to being convertible to any pointer type (but not to any integral type) also has its own type std::nullptr_t. So it is possible to have a method overload that accepts std::nullptr_t.

Exactly why is such an overload required?

解决方案

If more than one overload accepts a pointer type, an overload for std::nullptr_t is necessary to accept a nullptr argument. Without the std::nullptr_t overload, it would be ambiguous which pointer overload should be selected when passed nullptr.

Example:

void f(int *intp)
{
    // Passed an int pointer
}

void f(char *charp)
{
    // Passed a char pointer
}

void f(std::nullptr_t nullp)
{
    // Passed a null pointer
}

这篇关于std :: nullptr_t类型的用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆