🐭 C++ 中的空指针

在 C++ 的特定上下文中 0, NULL 都可以用来表示空指针,而且对于 NULL 的定义是由各家编译器的具体实现来决定的,这个一直比较混乱。C++11 中的 nullptr_t 类型为空指针引入了单独的明确的强类型定义,结束了空指针的混乱局面。我们应该尽可能的在一切需要空指针的地方无脑的使用 nullptr 来避免空指针类型混乱问题。

  1. #include <iostream>
  2. int main(void) {
  3. // $ clang++ -v
  4. // Apple clang version 11.0.3 (clang-1103.0.32.59)
  5. // Target: x86_64-apple-darwin19.4.0
  6. // Thread model: posix
  7. // InstalledDir: /Library/Developer/CommandLineTools/usr/bin
  8. using std::cout, std::is_same_v;
  9. cout.setf(std::ios_base::boolalpha);
  10. auto n = NULL;
  11. // NULL is defined as 0L in my clang++ (c++ mode)
  12. // NULL is defined as ((void*)0) in my clang (c mode)
  13. cout << is_same_v<decltype(n), long> << '\n'; // true
  14. long x = 0; // 0 is int
  15. long *px = 0; // 0 is null pointer
  16. long y = NULL; // NULL is long
  17. long *py = NULL; // NULL is null pointer
  18. // long z = nullptr; // not compiles
  19. long *pz = nullptr; // nullptr is null pointer
  20. cout << is_same_v<decltype(nullptr), std::nullptr_t> << '\n'; // true
  21. cout << is_same_v<decltype(pz), long *> << '\n'; // true
  22. return 0;
  23. }

:::tips std::nullptr_t 是由 nullptr 循环定义而来,nullptr 可以表示任何类型的空指针类型 :::

🐂 空指针与函数重载

由于 0, NULL 在特定上下文中具有二义性,对于函数重载来说,二义性会导致重载决议问题。另外,当函数模板涉及到指针类型推导时只有 nullptr 会推导为空指针。

对于一般函数重载来说,就是匹配最佳参数:
代码示例 :

  1. #include <iostream>
  2. void f(int) { std::cout << "calls f(int)\n"; };
  3. void f(long) { std::cout << "calls f(long)\n"; };
  4. void f(bool) { std::cout << "calls f(bool)\n"; };
  5. void f(void *) { std::cout << "calls f(void *)\n"; };
  6. int main(void) {
  7. // $ clang++ -v
  8. // Apple clang version 11.0.3 (clang-1103.0.32.59)
  9. // Target: x86_64-apple-darwin19.4.0
  10. // Thread model: posix
  11. // InstalledDir: /Library/Developer/CommandLineTools/usr/bin
  12. f(0); // calls f(int)
  13. f((void *)0); // calls f(void *)
  14. f((bool)0); // calls f(bool)
  15. f(NULL); // calls f(long)
  16. f(nullptr); // calls f(void *)
  17. return 0;
  18. }

:::tips ‘0’ 的最佳匹配当然是 ‘int’
‘NULL’ 的最佳匹配当然是 ‘long’
‘nullptr’ 的最佳匹配当然是 ‘void *’,当然也可以匹配其他任何指针类型 :::

对于模板重载来说,涉及到类型推导:
代码示例 :

  1. #include <iostream>
  2. // shared pointer
  3. int f1(std::shared_ptr<int> sp) { return 1; };
  4. // unique pointer
  5. double f2(std::unique_ptr<int> up) { return 1.0; };
  6. // raw pointer
  7. bool f3(int *rp) { return true; };
  8. template <typename FuncType, typename PtrType>
  9. decltype(auto) call(FuncType func, PtrType ptr) {
  10. return func(ptr);
  11. }
  12. int main(void) {
  13. // both works without template
  14. f1(0), f2(0), f3(0);
  15. f1(NULL), f2(NULL), f3(NULL);
  16. f1(nullptr), f2(nullptr), f3(nullptr);
  17. // since 0 is int, and NULL is long, below two lines of code not compiles
  18. // call(f1, 0), call(f2, 0), call(f3, 0);
  19. // call(f1, NULL), call(f2, NULL), call(f3, NULL);
  20. call(f1, nullptr), call(f2, nullptr), call(f3, nullptr);
  21. return 0;
  22. }

:::tips 函数模板指针类型推导,’0’ 被推导为 ‘int’,’NULL’ 被推导为 ‘long’,只有 ‘nullptr’ 会被推导为广义空指针 :::

🐯 总结

  1. 总是使用 nullptr 来替代 0, NULL 表示空指针
  2. 尽量不要设计形参为整型与指针的重载函数,避免误用