android - 为什么 clang 将 NULL 定义为 __null?

标签 android c++ android-ndk clang++ llvm-clang

我有一个 .cpp 文件,它在几个地方使用了 NULL。当我尝试在 Windows 机器 + 独立工具链上使用 clang++ 为 Android/x86 平台编译此 cpp 文件时,我在使用 NULL 的地方遇到了“预期表达式”错误。我在 Android NDK 提供的 clang header 的 stddef.h 中找到了 NULL 的定义,如下所示。

#if defined(__need_NULL)
#undef NULL
#ifdef __cplusplus
#  if !defined(__MINGW32__) && !defined(_MSC_VER)
#    define NULL __null
#  else
#    define NULL 0
#  endif
#else
#  define NULL ((void*)0)
#endif

据我所知,__null 是特定于 GNU 编译器的。在我的例子中,_MSC_VER 和 __MINGW32__ 都是未定义的,因为我正在使用 clang++ 和独立工具链为 Android 平台编译。所以它进入了 define NULL __null。由于 clang++ 不知道 __null 是什么,因此会导致“预期表达式”错误。

我的问题是,为什么 clang 使用 GNU 编译器提供的宏(如 __null)?或者我在这里遗漏了什么?

谁能帮我理解一下。谢谢

最佳答案

Why is clang defining NULL as __null?

__null优于0,因为前者只是一个空指针常量,而后者也是一个整型常量。这种差异在重载解析和类型推导的情况下很重要:

void foo(int);
void foo(void*);

foo(0);       // calls foo(int)
foo(__null);  // call is ambiguous, program is ill-formed
foo(NULL);    // could have either behaviour
              //  call to foo(int) would be undesirable
foo(nullptr); // calls foo(void*)

原因与C++11引入nullptr的原因相同或相似。

这样做的另一个原因是 clang 力求与 GCC 紧密兼容。

Since clang++ has no clue of what __null is

铛++ appears了解什么是 __null

关于android - 为什么 clang 将 NULL 定义为 __null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58698115/

相关文章:

android - 加快数据库写入

c++ - 如何检查一个变量是否不等于 C++ 中的多个东西?

C++计算文件中单词的出现次数并写入另一个文件

java - android ndk wrapped c++ file 获取android系统文件路径

android - 从 NDK 获取应用程序名称

android - 如何在 Android 中将图形置于相机预览之上?

java - worldToScreen() - 它是如何工作的?

android - Android LiveData : How to do an ArrayList<LiveData<RetrofitObject>>?

c++ - 在 C++ 中保存和加载大数组

android - 如何在多项目 Android 构建中排除重复的 C 共享库 (.so)?