C++中选择和迭代语句内部类型转换的过程
Process of conversion of types inside selection and iteration statements in C++
根据 C++ ISO:
The value of a condition that is an initialized declaration in a
statement other than a switch statement is the value of the declared
variable contextually converted to bool (7.3). If that conversion is
ill-formed, the program is ill-formed. The value of a condition that
is an initialized declaration in a switch statement is the value of
the declared variable if it has integral or enumeration type, or of
that variable implicitly converted to integral or enumeration type
otherwise. The value of a condition that is an expression is the value of the
expression, contextually converted to bool for statements other than switch; if that conversion is ill-formed,
the program is ill-formed.
以下引自上述第7.3节:
Certain language constructs require that an expression be converted to
a Boolean value. An expression e appearing in such a context is said
to be contextually converted to bool and is well-formed if and only if
the declaration bool t(e); is well-formed, for some invented temporary
variable t (9.4).
基于这两个,我得到一个想法 switch-statement
如果它具有适当的类型并不总是进行转换。否则 if-statement
看起来总是执行这样的转换,即使我做了类似 if (true){}
的事情,我知道 true
值会被转换。那么,这是怎么回事?代码:if(true){}
会将 true
转换为布尔值?(即使 true 已经是布尔值)
评论已经讨论过你误解了这两段。我将专注于第二个。重要信息如下:
在某些情况下,值可能会隐式进行转换,即使转换实际上只能显式进行。
一些代码示例可能会有所帮助:
struct foo{
explicit operator bool() {return true;}
};
int main()
{
foo f;
bool b(f); // fine !
bool c = f; // error! no viable conversion from f to bool
// because foo::operator bool is explicit
}
A foo
可以转换为 bool
但转换运算符是 explicit
。因此 bool b(f)
(显式转换)是好的,而 bool c = f;
(隐式转换)不是。
现在,在某些情况下...
An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (9.4).
这解释了一些特殊的转换情况。这种转换是隐式发生的,但恰好在显式转换格式正确时格式正确。
如果没有这样的“上下文转换”,这将是一个编译器错误:
foo f;
if (f) {}
但是,因为 f
可以根据上下文转换为 bool
,所以代码没问题。如果没有这个特殊的上下文转换规则,就必须写 if (bool(f))
因为 foo::operator bool
是 explicit
.
换句话说,该段落不是关于 bool
转换为 bool
,而是解释了在必要时应用的通常隐式/显式转换的异常。
根据 C++ ISO:
The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable contextually converted to bool (7.3). If that conversion is ill-formed, the program is ill-formed. The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise. The value of a condition that is an expression is the value of the expression, contextually converted to bool for statements other than switch; if that conversion is ill-formed, the program is ill-formed.
以下引自上述第7.3节:
Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (9.4).
基于这两个,我得到一个想法 switch-statement
如果它具有适当的类型并不总是进行转换。否则 if-statement
看起来总是执行这样的转换,即使我做了类似 if (true){}
的事情,我知道 true
值会被转换。那么,这是怎么回事?代码:if(true){}
会将 true
转换为布尔值?(即使 true 已经是布尔值)
评论已经讨论过你误解了这两段。我将专注于第二个。重要信息如下:
在某些情况下,值可能会隐式进行转换,即使转换实际上只能显式进行。
一些代码示例可能会有所帮助:
struct foo{
explicit operator bool() {return true;}
};
int main()
{
foo f;
bool b(f); // fine !
bool c = f; // error! no viable conversion from f to bool
// because foo::operator bool is explicit
}
A foo
可以转换为 bool
但转换运算符是 explicit
。因此 bool b(f)
(显式转换)是好的,而 bool c = f;
(隐式转换)不是。
现在,在某些情况下...
An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (9.4).
这解释了一些特殊的转换情况。这种转换是隐式发生的,但恰好在显式转换格式正确时格式正确。
如果没有这样的“上下文转换”,这将是一个编译器错误:
foo f;
if (f) {}
但是,因为 f
可以根据上下文转换为 bool
,所以代码没问题。如果没有这个特殊的上下文转换规则,就必须写 if (bool(f))
因为 foo::operator bool
是 explicit
.
换句话说,该段落不是关于 bool
转换为 bool
,而是解释了在必要时应用的通常隐式/显式转换的异常。