detailed answer을 추가하는 동안 Visual C++에서 불평하는 동안 GCC가 다음 코드를 경고하지 않는 것으로 나타났습니다. 레드햇 (리눅스)의 Cygwin에 GCC가 std :: strrchr() 반환 값에서 'const char *'에서 'char *'로의 변환을 허용하는 이유는 무엇입니까?
- 4.1.2 : 나는 세 가지 다른 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'; }
윈도우)
그러나 경고/오류없이이 코드를 컴파일이 모든 세 GCC 버전 :
,536,913 63,210> g++ -Wall -Wextra -pedantic -ansi test.cpp && echo "success"
success
는 마이크로 소프트 컴파일러 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
이로
(내 사무실에서, 나는 다른 버전을 사용하여 테스트 할 수 ideone/codepad/...에 액세스 할 수 없습니다) 코드는 std::strrchr을 사용합니다. 왜 GCC가 불평하지 않는지 이해할 수 없습니다.
const char* strrchr(const char* str, int ch); //the code above uses this declaration
char* strrchr( char* str, int ch);
내 질문 : 왜 g ++ 성공적으로 경고/오류없이이 코드를 컴파일 무엇입니까? 그게 버그 야? 특징? 내 편에 놓친 구성?
이상한 ... gcc 4.7.2 http://ideone.com/7rcwVE 오류 'const char *'에서 'char *'[-fpermissive]'로의 변환이 잘못되었지만 gcc 4.3.2 http :// /ideone.com/fDdZ08 오류가 없습니다. 오래된 컴파일러 버그? 'g ++ -Wall -Wextra -pedantic -ansi file.cpp' (또는이 옵션들의 부분 집합)을 시도해 볼 수 있습니까? –
그래서 무엇이 문제입니까? 또한, 내 버전의 GCC (4.6.3)에서 예상되는 오류가 발생합니다. –