c++ - g++ 关于零文字的不一致

标签 c++ g++ clang++

如标题所示,g++编译器在接受零文字赋值时似乎不一致,我想请教专业人士是什么原因。 对于 C++11 之前的标准,此代码(除了 nullptr 关键字)是有效的。 clang 看起来至少好很多。这是一个应该修复的持续性错误吗?感谢您的解释。

#include <cstddef>

struct A{
  int var;
  int A::* a;
};

int main(){
  A a;
  a.a = &A::var;    // Obviously compiles.
  a.a = nullptr; 
  a.a = NULL;       // Should be the same as nullptr as for C++11+
  a.a = 0;          // Conversion from integer zero literal to pointer is allowed
  a.a = (int)0;     // This is not allowed? I guess one step of indirection ruins exception rule from line above
  a.a = (int)'\0';  // Compiles on g++, what?
  a.a = (char)0;    // Doesn't compile.
  a.a = (char)'\0'; // Doesn't compile
  // All of this compiles on g++.
  a.a = (short)0;
  a.a = (long)0;
  a.a = (long long)0;
  a.a = (long long)0x0;
  a.a = (long long)0b0;
}

结果:

g++ (GCC) 8.2.1 20181127 // All stds as flags C++11+
/tmp/test.cpp: In function ‘int main()’:
/tmp/test.cpp:14:14: error: cannot convert ‘int’ to ‘int A::*’ in assignment
   a.a = (int)0; // This is not allowed? I guess one step of indirection ruins exception rule from line above
              ^
/tmp/test.cpp:16:15: error: cannot convert ‘char’ to ‘int A::*’ in assignment
   a.a = (char)0; // Doesn't compile.
               ^
/tmp/test.cpp:17:15: error: cannot convert ‘char’ to ‘int A::*’ in assignment
   a.a = (char)'\0'; // Doesn't compile
------------------------------------------------------------------------------------------------------------------------
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516 // All stds as flags C++11+
test.cpp: In function ‘int main()’:
test.cpp:14:14: error: cannot convert ‘int’ to ‘int A::*’ in assignment
   a.a = (int)0; // This is not allowed? I guess one step of indirection ruins exception rule from line above
              ^
test.cpp:17:15: error: cannot convert ‘char’ to ‘int A::*’ in assignment
   a.a = (char)'\0'; // Doesn't compile
               ^~~~              ^~~~
------------------------------------------------------------------------------------------------------------------------
clang version 7.0.1 (tags/RELEASE_701/final) // All stds as flags C++11+ 
Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0) // All stds as flags C++11+ 
/tmp/test.cpp:14:9: error: assigning to 'int A::*' from incompatible type 'int'
  a.a = (int)0; // This is not allowed? I guess one step of indirection ruins exception rule from line above
        ^~~~~~
/tmp/test.cpp:15:8: error: assigning to 'int A::*' from incompatible type 'int'
        a.a = (int)'\0'; // Compiles, what?
              ^~~~~~~~~
/tmp/test.cpp:16:9: error: assigning to 'int A::*' from incompatible type 'char'
  a.a = (char)0; // Doesn't compile.
        ^~~~~~~
/tmp/test.cpp:17:9: error: assigning to 'int A::*' from incompatible type 'char'
  a.a = (char)'\0'; // Doesn't compile
        ^~~~~~~~~~
/tmp/test.cpp:19:9: error: assigning to 'int A::*' from incompatible type 'short'
  a.a = (short)0;
        ^~~~~~~~
/tmp/test.cpp:20:8: error: assigning to 'int A::*' from incompatible type 'long'
        a.a = (long)0;
              ^~~~~~~
/tmp/test.cpp:21:8: error: assigning to 'int A::*' from incompatible type 'long long'
        a.a = (long long)0;
              ^~~~~~~~~~~~
/tmp/test.cpp:22:8: error: assigning to 'int A::*' from incompatible type 'long long'
        a.a = (long long)0x0;
              ^~~~~~~~~~~~~~
/tmp/test.cpp:23:8: error: assigning to 'int A::*' from incompatible type 'long long'
        a.a = (long long)0b0;
              ^~~~~~~~~~~~~~
9 errors generated.

最佳答案

DR 903之后,不是整型文字的整型常量表达式不再被视为空指针常量,因此 a.a = (int)0; (包括)之后的所有行均无效。 GCC 错误地接受了其中一些,并且已经有一些与此问题相关的错误报告( 5970477712 )。

关于c++ - g++ 关于零文字的不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54051664/

相关文章:

c++ - 什么是 clang++ 选项以便在 GDB 中我可以使用 std::cout 作为函数参数

c++ - 在这个例子中是否需要重载的 operator= ?

c++ - 为什么new调用了3次,delete调用了3次?

c++ - 多个嵌套 for 循环与单个 for 循环

gcc - MacOS High Sierra上的链接错误: “Undefined symbols for architecture x86_64” std::__1

c++ - 在Mac mojave上安装的gcc和g++无法正常工作(简装)

c++ - static const Color 不完全适用于 Allegro5

MacOS 上的 C++ : show date and time issue

c++ - 从临时返回常量对象和构造

c++ - 彩虹表 : Unable to get last reduction