gpt4 book ai didi

c++ - 当条件运算符用于 cv 非限定非数组对象类型的两个 xvalue 时,是否可以生成 cv 限定、数组或函数类型?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:51:20 28 4
gpt4 key购买 nike

给定两个 cv-unqualified 非数组对象类型 T1T2 , 可以表达 true ? std::declval<T1>() : std::declval<T2>()曾经有过 cv 限定的数组或函数类型吗?我很确定它不能,但我想确保我没有遗漏任何东西。


动机:当前提议的决议LWG issue 2465不会衰减 true? std::declval<D1>() : std::declval<D2>() 形式的条件表达式的类型, 其中D1D2std::decay 生产(因此是 cv-unqualified 非数组对象类型*)。只有当衰减条件表达式的类型没有影响时,这是正确的(如果类型是 cv 限定的、数组或函数类型**,则不正确)。

* 忽略“异常函数类型”的情况,无论如何都不会产生有效的表达式,以及 void案例,这是一个单独的问题。
** 根据 [expr]/5 , 表达式从来没有引用类型。

最佳答案

我们可以使用 [expr.cond] 证明这一点:

  1. If either the second or the third operand has type void, [...]

第一个子项目符号将throw-expression作为操作数,第二个子项目符号给出了结果类型void

  1. Otherwise, if the second and third operand are glvalue bit-fields of the same value category and of types cv1 T and cv2 T, respectively, the operands are considered to be of type cv T for the remainder of this section, where cv is the union of cv1 and cv2.

空集的并集是空的,所以这里不用担心。

  1. Otherwise, if the second and third operand have different types and either has (possibly cv-qualified) class type, or if both are glvalues of the same value category and the same type except for cv-qualification, an attempt is made to form an implicit conversion sequence (13.3.3.1) from each of those operands to the type of the other.

让我们分析以下每个案例:

  • (4.1) If E2 is an lvalue, the target type is “lvalue reference to T2, [..].
  • (4.2) If E2 is an xvalue, the target type is “rvalue reference to T2, [..].

表达式因此属于 T2 类型,cv-不符合假设条件。

  • (4.3) If E2 is a prvalue or if neither of the conversion sequences above can be formed and at least one of the operands has (possibly cv-qualified) class type:
    • if T1 and T2 are the same class type (ignoring cv-qualification), or one is a base class of the other, and T2 is at least as cv-qualified as T1, the target type is T2,
    • otherwise, the target type is the type that E2 would have after applying the lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions.

在第一种情况下,假设目标类型是 cv-unqualified。

最后,l-t-r 转换不添加 cv 限定([conv.lval]/1)。并且 *-to-pointer 转换也不能做到这一点;实际上,它们在应用程序后产生的是指针(即非数组、非类)类型的纯右值,根据 [expr]/6从来没有 cv 资格。因为 E2T2 类型,所以在这种情况下目标类型也是 cv-unqualified。

关于c++ - 当条件运算符用于 cv 非限定非数组对象类型的两个 xvalue 时,是否可以生成 cv 限定、数组或函数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38933823/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com