Cast literal warning against char * char *

I cannot find a warning for the following in Visual Studio. I turned on / Wall but got nothing:

const char * pointer = '\0'; 

gcc will not compile it for C ++ 11, C ++ 14 or C ++ 17:

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

gcc compiles with the warning above if I pass -fpermissive :

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

clang will not compile for C ++ 11, C ++ 14 or C ++ 17:

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

I ask because of the code below, which ended up in our code base, apparently without warning:

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

Is there a way to enable a warning in Visual Studio?

+5
source share
1 answer

Visual Studio 2015 allows this conversion with constant values โ€‹โ€‹of '\ 0'. Examples:

 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) 

Specific error:

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

Apple LLVM 8.1.0 (clang-802.0.41) gives a warning with C ++ 03, but with C ++ 11 and later. The behavior was changed once between February 28, 2011 (project N3242 and May 15, 2013 (project N3690). I cannot find the exact point.

In earlier C ++ drafts, such as n1905, the OP code is defined as a valid conversion:

The null pointer constant is the integral constant expression (5.19) of the rvalue of an integer type that evaluates to zero. A null pointer constant value can be converted to a pointer type; the result is the null value of a pointer of this type and can be distinguishable from any other value of a pointer to an object or a pointer to a function type. Two null pointer values โ€‹โ€‹of the same type should compare equal. Converting a null pointer constant to a pointer to a type with qualification cv is one conversion , not a sequence of conversion of a pointer followed by qualification conversion (4.4).

Section 3.9.1.2 defines signed integer types:

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

This has been changed in subsequent drafts. 2013 draft N3690 in section 4.10 states:

The null pointer constant is an integer literal (2.14.2) with a value of 0 or a prdue value of type std :: nullptr_t. The null pointer constant can be converted to a pointer type; the result is the null value of the pointer of this type and is different from any other value of the object pointer or the type of the function pointer. This conversion is called null pointer conversion. Two null pointer values โ€‹โ€‹of the same type are compared equal. The conversion of a null pointer constant to a pointer to a type with qualification cv is one conversion , not a sequence of conversion of a pointer followed by qualification conversion (4.4).

character-literal defined as a literal in section 2.14.1, but it is not specified in section 2.14.2. Instead, he gets his own section - 2.14.3.

C ++ 17 draft n4659 has the same phrase, but in different sections.

I see no way to give a warning for this in VS 2015. This would be another reason to run the static analysis tools / other compilers to catch additional warnings.

Thanks @EricPostpischil for the help.

+1
source

Source: https://habr.com/ru/post/1273069/


All Articles