Как и в заголовке, компилятор g ++ кажется непоследовательным, когда речь идет о принятии нулевого литерального присваивания, и я хотел бы спросить профессионалов, в чем причина.
В стандарте preC ++ 11 этот код (кроме ключевого слова nullptr) действителен. кажется, что лязг намного лучше. Это постоянная ошибка, которая должна быть исправлена? Спасибо за объяснение.
#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.