gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:04:11 24 4
gpt4 key购买 nike

如题,g++编译器在接受零字面量赋值时似乎不一致,想请教专业人士是什么原因。对于 preC++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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com