2017-11-02 6 views
5

Visual Studio에서 다음에 대한 경고를 찾을 수 없습니다. 나는에/벽 켜져 있지만 여전히 아무것도 얻을 :char 리터럴에서 char 로의 캐스트에 대한 경고 *

const char * pointer = '\0'; 

GCC는 C++ 11을 컴파일되지 않습니다, C++ (14), 또는 C++ 17 :

[x86-64 gcc 7.2 #1] error: invalid conversion from 'char' to 'const char*' [-fpermissive]

gcc가 컴파일됩니다 I가 통과하면 경고로서 상기와 -fpermissive :

[x86-64 clang 5.0.0 #1] error: cannot initialize a variable of type 'const char *' with an rvalue of type 'char'

0

[x86-64 gcc 7.2 #1] warning: invalid conversion from 'char' to 'const char*' [-fpermissive]

연타는 C++ 11 ++ 14 C 또는 C++ 17 컴파일되지

내가 분명히 경고없이 때문에 우리의 코드베이스에 결국 아래 코드의 부탁 해요 :

std::ofstream file; 
//... 
file.write('\0', 20); 

비주얼 스튜디오에서 이것에 대한 경고를 켤 수있는 방법이 있습니까?

답변

1

Visual Studio 2015는 '\ 0'의 const 값으로 만이 변환을 허용합니다. 예 :

char c = '\0'; 
const char cconst = '\0'; 

const char * p1 = c;  //error (not even warning) 
const char * p2 = cconst; //ok 
const char * p3 = '\0'; //ok 
const char * p4 = '\1'; //error (not even warning) 

특정 오류는 다음과 같습니다

Error: a value of type "char" cannot be used to initialize an entity of type "const char *"

애플 LLVM 8.1.0은 (그 소리-802.0.41)는 C++ 03과 경고하지만 C++ 11에 오류를 제공합니다 후에. 동작은 2011 년 2 월 28 일 사이에 변경되었습니다 (N3242 초안과 2013 년 5 월 15 일 초 (N3690 초안) 정확한 지점을 찾을 수 없습니다.)

n1905와 같은 이전 초안에서는 OP

A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4).

제 3.9.1.2 정의하는 부호있는 정수 유형 : 코드가 유효한 전환으로 정의된다.

There are five signed integer types: “signed char”, “short int”, “int”, “long int”, and “long long int”.

이 나중에 초안 변경되었습니다 초안 N3690에서 2013 년 4.10 절은 말합니다 :

A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4).

character-literal은 섹션 2.14.1에서 리터럴로 정의되지만 섹션 2.14.2에는 나타나지 않습니다. 대신 2.14.3 절을 따릅니다.

C++ 17 초안 n4659는 완전히 다른 표현을 사용하지만 다른 섹션에 있습니다.

VS 2015에서 경고 메시지를 표시하는 방법이 없습니다. 정적 분석 도구/다른 컴파일러를 실행하여 추가 경고를받는 또 다른 이유가 될 수 있습니다.

도움을 주신 @EricPostpischil에게 감사드립니다.

관련 문제