在编译BoringSSL时,我发现gcc和clang在行为上存在差异,并能够将其简化为下面的测试用例来说明:
typedef char *OPENSSL_STRING;
#if USE_TYPEDEF
#define constptr const OPENSSL_STRING
#else
#define constptr const char *
#endif
int
foo (const void **ap)
{
constptr a = (constptr) *ap;
return a != 0;
}我测试了以下四种场景:
sh$ g++ -c t2.cc -Wignored-qualifiers -DUSE_TYPEDEF
t2.cc: In function ‘int foo(const void**)’:
t2.cc:11:30: warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]
11 | constptr a = (constptr) *ap;
| ^~
sh$ g++ -c t2.cc -Wignored-qualifiers
sh$ clang++ -c t2.cc -Wignored-qualifiers -DUSE_TYPEDEF
sh$ clang++ -c t2.cc -Wignored-qualifiers
sh$ 这是gcc的问题吗?还是还有什么事情我不明白?
供参考:警告为在BoringSSL的stack.h中
发布于 2021-02-26 15:08:07
在给定const OPENSSL_STRING时,const本身是限定类型OPENSSL_STRING的,因此类型是char * const,即指向非const char的const指针(请注意,它不是const char *)。Gcc只是想告诉你,作为演员的结果,const部分被忽略了。也就是说,(char * const) *ap;具有与(char *) *ap;相同的效果。
将类型更改为int可能会更清楚。
const int i = (int) 0; // a weird conversion
const int i = (const int) 0; // same effect as abovehttps://stackoverflow.com/questions/66388124
复制相似问题