[英]Why does GCC accept convertion from 'const char *' to 'char *' on std::strrchr() returned value?
While adding a detailed answer , I noticed that GCC does not warn the following code while Visual C++ complains. 在添加详细答案的同时 ,我注意到Visual C ++抱怨时,GCC不会警告以下代码。
#include <cstring>
int main()
{
const char CONSTSTR[] = "foo/bar/foobar.txt";
char *nonconst = std::strrchr (CONSTSTR, '/');
// cannot convert from 'const char *' to 'char *'
*nonconst++ = 'B';
*nonconst++ = 'A';
*nonconst++ = 'D';
}
I have tested three different GCC versions: 我已经测试了三种不同的GCC版本:
But all these three GCC versions compiled this code without any warning/error: 但是所有这三个GCC版本都在没有任何警告/错误的情况下编译了此代码:
> g++ -Wall -Wextra -pedantic -ansi test.cpp && echo "success"
success
While Microsoft compiler v16 complains: 尽管Microsoft编译器v16抱怨:
> cl -c test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
test.cpp(5) : error C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
Conversion loses qualifiers
(from my office, I do not have access to ideone/codepad/... to test it using other versions) (在我的办公室,我无权使用ideone /键盘/ ...来使用其他版本进行测试)
As this code uses std::strrchr , I do not understand why GCC does not complain. 由于此代码使用了std :: strrchr ,所以我不明白为什么GCC不会抱怨。
const char* strrchr( const char* str, int ch ); //the code above uses this declaration
char* strrchr( char* str, int ch );
My question: Why does g++ successfully compile this code without any warning/error? 我的问题: 为什么g ++成功编译此代码而没有任何警告/错误? Is it a bug?
是虫子吗? a feature?
一个特征? a miss-configuration on my side?
我这边的配置错误?
Actually your g++ does not accept the conversion from ' const char *
' to ' char *
', it's just that on your version std::strrchr()
returns a char*
(incorrectly, instead of a const char*
). 实际上,您的g ++不接受从'
const char *
'到' char *
'的转换,只是在您的版本上std::strrchr()
返回char*
(错误地,而不是const char*
)。
To verify the first part of my statement, try to compile the following on your GCC versions, I predict that all will correctly issue an error: 为了验证我的陈述的第一部分,请尝试在您的GCC版本上编译以下内容,我预计所有内容都会正确发出错误:
int main()
{
const char* p = "foo";
char* q = p; // error, invalid conversion from 'const char*' to 'char*'
}
Now for the second part, I tried to compile the following minimal code, whose actual aim is to trigger an error in order to list the declared overloads of std::strrchr
: 现在,对于第二部分,我尝试编译以下最少的代码,其实际目的是触发错误 ,以便列出已声明的
std::strrchr
重载 :
#include <cstring>
void (*p)() = &std::strrchr; // error here, with "candidates are: ..."
int main() {}
Well, with gcc 4.7.2 the message shows the expected "all non-const" and "all const" overloads: 好吧,在gcc 4.7.2中,该消息显示了预期的“所有非常量”和“所有const”重载:
prog.cpp:2:21: error: no matches converting function ‘strrchr’ to type ‘void (*)()’
In file included from /usr/include/c++/4.7/cstring:44:0,
from prog.cpp:1:
/usr/include/string.h:249:1: error: candidates are: char* strrchr(char*, int)
/usr/include/string.h:255:1: error: const char* strrchr(const char*, int)
ie the prototypes 即原型
char* strrchr( char* , int );
const char* strrchr( const char* , int ); // Question's code will use this one (-> error)
But with gcc 4.3.2 the message was different: 但是对于gcc 4.3.2 ,消息却有所不同:
prog.cpp:2: error: no matches converting function 'strrchr' to type 'void (*)()'
/usr/include/string.h:171: error: candidates are: char* strrchr(const char*, int)
/usr/include/c++/4.3/cstring:118: error: char* std::strrchr(char*, int)
ie the overloads were 即超载是
char* strrchr( const char* , int ); // Question's code would use this one (-> no error...)
char* strrchr( char* , int );
(the second one is the C++ non-const overload; but the first one is the old C version , and should instead be the C++ const overload). (第二个是C ++非常量重载;但是第一个是旧的C版本 ,而应该是C ++ const重载)。
This it seems that the headers ( <cstring>
and/or <string.h>
) were incorrect on this version, and I suspect that it's the same on yours. 看来此版本的标头(
<cstring>
和/或<string.h>
)不正确,我怀疑您的标头是相同的。
Edit: I found for example a discussion , a blog post and a bug report (for strchr
not strrchr
but it's the same story). 编辑:我发现例如讨论 ,一个博客帖子和bug报告 (对
strchr
不strrchr
但它是同样的故事)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.