本文介绍了false隐式转换为空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否允许将 false 隐式转换为指针在clang ++和g ++之间是不同的:

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

g ++ - 4.8:总是带或不带-std = c ++ 11的警告

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

clang ++(trunk):如果没有-std = c ++ 11, if with -std = c ++ 11

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

所以任何人都知道为什么g ++和clang ++的行为不同,谁是正确的? C ++标准(C ++ 03和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.

感谢。

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


推荐答案

右侧:

6 bool类型的值为 true false 。 [注意:没有有符号,无符号,短或长bool类型或值。 - end
note]类型bool的值参与积分促销(4.5)。

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 没有值零,因此无法转换为空指针:

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

1空指针常数是一个整数常数表达式(5.19)整数类型的prvalue strong>或
的prvalue类型std :: nullptr_t。空指针常量可以转换为
指针类型;

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;

可能建议由积分提升code> bool 到 int )和空指针转换,但它不会有效:

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

在意义。
第4条列举了这些转换的完整集合。标准
转换序列是
中顺序之后的标准转换序列:

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:


  • 零个或一个转换以下集合:lvalue到rvalue的转换,数组到指针的转换和函数到指针
    的转换。

  • 零或一个转换来自以下集合:整数促销,浮点促销,积分转换,浮动
    点转化,浮动积分转换,指针
    转化
    ,指向成员转换的指​​针和布尔转换。

  • 零或一个资格转换。

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

[注意:标准转换序列可以为空,即它可以
由无转换组成。 - end note]如果需要,标准转换序列
将应用于表达式,以将其转换为
所需的目标类型。

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

这篇关于false隐式转换为空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:56