【发布时间】:2020-04-20 02:30:29
【问题描述】:
由于[expr.cond]部分太长,这里只引用第6节和第7节的一部分,其他部分在链接[expr.cond]中给出
6.否则,结果为纯右值。如果第二个和第三个操作数的类型不同,并且任何一个都具有(可能是 cv 限定的)类类型,则使用重载决议来确定要应用于操作数的转换(如果有)([over.match.oper ],[过度构建])。如果重载决议失败,则程序格式错误。否则,将应用由此确定的转换,并在本节的其余部分中使用转换后的操作数代替原始操作数。
7.对第二个和第三个操作数执行左值到右值、数组到指针和函数到指针的标准转换。在这些转换之后,应满足以下条件之一:
7.1 第二个和第三个操作数的类型相同;结果属于该类型,并且使用选定的操作数初始化结果对象。
7.2 第二和第三个操作数有算术或枚举类型;执行通常的算术转换以将它们转换为通用类型,结果就是该类型。
让我们考虑以下情况:
#include <iostream>
int main(){
bool b = true;
int a = 0;
auto r = b?'c':a; //#1
}
我们知道表达式 #1 不符合 [expr.cond]/2,[expr.cond]/3,[expr.cond]/4,[expr.cond]/5 并且我们知道它是[expr.cond]/6 的情况,但是只有 如果第二个和第三个操作数不具有相同类型,并且任何一个具有(可能是 cv-qualified)类类型,并且重载解析成功的条件 满意,那么第 7 节将在操作数上执行,因为我阅读第 6 节。显然,'c' 和 'a' 都不是类类型。所以,我想知道第 7 节是否继续在这些上执行操作数,尤其是第 7.2 节。如果我误解了,标准如何使第 7 节执行案例#1?
解读:
我对第6句的理解好像是:
if("the second and third operands do not have the same type, and either has (possibly cv-qualified) class type" == true){
if("the overload resolution fails"==true){
"the program is ill-formed"
}else{
"the converted operands are used in place of the original operands for the
remainder of this section" //that means sentence 7 will be performed
}
}
【问题讨论】:
-
如果操作数不是类类型,为什么你决定 7 不适用?
-
@LanguageLawyer 因为,*if 之后的句子是条件,如果条件不满足。我认为条件之后的句子不执行。跨度>
-
@LanguageLawyer 换句话说,我对第 6 句的理解好像是:
if("second and third operands do not have the same type, and either has (possibly cv-qualified) class type"){ if("overload resolution fails"){ the program is ill-formed }else{ the converted operands are used in place of the original operands for the remainder of this section } } -
您的理解对我来说似乎是正确的。我不明白为什么前面段落中的错误“IF”条件并没有阻止您进一步阅读,而第六段中的条件以某种方式阻止您继续阅读第七段。
-
@LanguageLawyer 因为
true?'c':a不是前面段落所涵盖的情况,所以是第6句的情况,因为Otherwise在第6句中
标签: c++ c++17 language-lawyer