如果有专门的函数和模板函数,为什么没有必要专门研究`std :: nullptr_t`

iBu*_*Bug 3 c++ null nullptr language-lawyer overload-resolution

请考虑以下代码:

#include <iostream>
using namespace std;

void fun(const char* s){
    if (s == nullptr) {
        puts("const char* nullptr");
    } else {
        printf("%s\n", s);
    }
}

template <typename T>
void fun(T* p){
    printf("%p\n", p);
}

int main() {
    int a;
    fun("abc"); // Resolves to fun(const char*)
    fun(&a); // Specializes the template to int*
    fun(nullptr); // Uses fun(const char*)??
    fun(NULL); // Same as above
}
Run Code Online (Sandbox Code Playgroud)

我很诧异g++ 7.2.0不会抛出不明的重载解析错误,因为我认为nullptrNULL可以融入任何指针类型,其中包括fun(int*)专门从模板,只要不是专门用于过载std::nullptr_t.

为什么fun(nullptr)fun(NULL)直接解析fun(const char *)

krz*_*zaq 5

std::nullptr_t不是指针,因此它不会T*在函数模板中进行模式匹配.

由于这是违反直觉的,因此以下断言不会触发:

static_assert(std::is_pointer<std::nullptr_t>() == false);
Run Code Online (Sandbox Code Playgroud)

至于NULL它,它是一个实现定义的宏.如果要相信cppreference,它可以是值为零的整数文字(因此不是指针),也可以是类型的prvalue,std::nullptr_t如上所述.这是不是一个void*指针.