2017-11-02 52 views
5

Nie mogę znaleźć ostrzeżenia dla następujących w Visual Studio. Włączyłem/ścianie, ale wciąż nic:Ostrzeżenie dotyczące rzutowania z literału char na znak *

const char * pointer = '\0'; 

gcc nie będzie go skompilować dla C++ 11, C++ 14, C++ lub 17:

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

GCC skompiluje z powyższym, jako ostrzeżenie, jeśli przechodzą -fpermissive:

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

szczęk nie kompilacji C++ 11 ++ 14 C lub C++, 17:

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

Pytam ponieważ poniżej kod, który zakończył się w naszym kodzie, najwyraźniej bez ostrzeżeń:

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

Czy istnieje sposób, aby włączyć ostrzeżenia za to w Visual Studio?

Odpowiedz

1

Program Visual Studio 2015 zezwala na tę konwersję tylko z wartością stałą "\ 0". Przykłady:

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) 

Specyficzny błędu jest:

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

Jabłko LLVM 8.1.0 (dzyń-802.0.41) daje ostrzeżenie z C++ 03, ale błąd z C++ 11 i później. Zachowanie zostało zmienione w okresie między 28 lutego 2011 r. (Projekt N3242 i 15 maja 2013 r. (Projekt N3690) .Nie mogę znaleźć dokładnego punktu:

We wcześniejszych wersjach C++, takich jak n1905, PO Kod jest zdefiniowany jako ważnego konwersji:

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).

pkt 3.9.1.2 definiuje podpisane typy integer:.

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

ten został zmieniony w późniejszych projektach w projekcie N3690 od 2013, sekcja 4.10 mówi:

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).

jest zdefiniowany jako literał w sekcji 2.14.1, ale nie pojawia się w sekcji 2.14.2. Zamiast tego otrzymuje własną sekcję - 2.14.3.

C++ 17 szkic n4659 ma dokładnie to samo słownictwo, ale w różnych sekcjach.

Nie widzę sposobu, aby ostrzec o tym w VS 2015. Byłby to kolejny powód, aby uruchomić narzędzia do analizy statycznej/inne kompilatory, aby złapać dodatkowe ostrzeżenia.

Dzięki @EricPostpischil za pomoc.

Powiązane problemy