正如标题所示,g ++编译器在接受零文字赋值时似乎不一致,我想问一下专业人士这是什么原因。 对于preC ++ 11标准,此代码(除了nullptr关键字)有效。 lang似乎至少好很多。它是应该修复的永久性错误吗?感谢您的解释。
#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.