加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在std :: common_type的实现中发现这种情况有什么意义?

发布时间:2020-12-16 10:23:38 所属栏目:百科 来源:网络整理
导读:以下是 std::common_type 的专业之一. template class T,class Ustruct common_typeT,U { typedef decltype(true ? declvalT() : declvalU()) type;}; 我的观点是条件陈述是不必要的.如果它检查为true,表达式是否总是会解析为declval T()?如果是这样,返回的
以下是 std::common_type的专业之一.

template <class T,class U>
struct common_type<T,U> {
    typedef decltype(true ? declval<T>() : declval<U>()) type;
};

我的观点是条件陈述是不必要的.如果它检查为true,表达式是否总是会解析为declval< T>()?如果是这样,返回的typedef如何证明定义…

“Determines the common type among all types T...,that is the type all T... can be implicitly converted to.”

有人可以解释条件陈述吗?对true的检查是多余的,并且条件总是会解析为第一个操作数,这是否准确?如果是这样,返回的类型如何满足定义?

解决方法

三元运算符的类型不仅仅是编译器知道必须采用的分支类型.如果存在这样的类型,则它是两个分支共有的类型.如果没有这种类型,则编译失败.三元运算符是唯一具有属性的运算符,它将转换为表达式的公共类型,而不是两个值上的表达式的结果.也就是说,三元运算符只是被滥用来获得通用类型.然而,这种情况并不重要.

标准中的相关部分是5.16 [expr.cond]第3段:

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 convert each of those operands to the type of the other.

该段落之后是如何提供常见类型的规则.否则指的是第二或第三表达式是throw-expression或者第二或第三表达式中的一个或两个具有类型void的情况.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读