Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

c++ - false implicitly convert to null pointer

Whether false is allowed to be implicitly converted to pointer is different between clang++ and g++:

g++-4.8: always a warning with or without -std=c++11

clang++ (trunk): a warning if without -std=c++11, and an error if with -std=c++11

So anyone knows why g++ and clang++ behaves differently, and who is correct? What paragraphs in C++ standard (both C++03 and C++11) talks about the situation.

Thanks.

[hidden ~]$ cat b.cpp
const char* f() { return false; }

[hidden ~]$ g++ -c b.cpp
b.cpp: In function ‘const char* f()’:
b.cpp:1:26: warning: converting ‘false’ to pointer type ‘const char*’ [-Wconversion-null]
 const char* f() { return false; }
                          ^
[hidden ~]$ g++ -std=c++11 -c b.cpp
b.cpp: In function ‘const char* f()’:
b.cpp:1:26: warning: converting ‘false’ to pointer type ‘const char*’ [-Wconversion-null]
 const char* f() { return false; }
                          ^
[hidden ~]$ clang++ -c b.cpp
b.cpp:1:26: warning: initialization of pointer of type 'const char *' to null from a constant boolean expression [-Wbool-conversion]
const char* f() { return false; }
                         ^~~~~
1 warning generated.
[hidden ~]$ clang++ -std=c++11 -c b.cpp
b.cpp:1:26: error: cannot initialize return object of type 'const char *' with an rvalue of type 'bool'
const char* f() { return false; }
                         ^~~~~
1 error generated.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'd say clang with C++11 is right:

3.9.1 Fundamental types [basic.fundamental]

6 Values of type bool are either true or false. [ Note: There are no signed, unsigned, short, or long bool types or values. — end note ] Values of type bool participate in integral promotions (4.5).

bool does not have value zero, so can not be converted to null pointer:

4.10 Pointer conversions [conv.ptr]

1 A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type;

One might suggest conversion sequence consisting of integral promotion (bool to int) and null pointer conversion, but it would not be valid:

4 Standard conversions [conv]

1 Standard conversions are implicit conversions with built-in meaning. Clause 4 enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:

  • Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.
  • Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.
  • Zero or one qualification conversion.

[ Note: A standard conversion sequence can be empty, i.e., it can consist of no conversions. — end note ] A standard conversion sequence will be applied to an expression if necessary to convert it to a required destination type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...