`
xuning2516
  • 浏览: 7615 次
  • 性别: Icon_minigender_1
  • 来自: 江西
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

C++ 条件运算符 ? :求公共类型

 
阅读更多

这段时间看C++标准库,看到有一个求公共类型的实用函数common_type<>;其实现是如下:

template<typename T1,typename T2>
struct common_type<T1,T2>
{
    typedef decltype(true?declval<T1>(),declval<T2>()) type;
}
刚开始还觉得奇怪,条件永远为true,不就是直接计算T1吗,想当然的以为产生公共类型不就是T1的类型吗?后然通过一个实例上机实验才发现不是这么回事。于是找到关于条件运算符的说明;我们知道条件运算符?:是C++中唯一的一个三目运算符。用法
expression ? expression : expression

下面是msdn对条件运算符的解释。from:http://msdn.microsoft.com/en-us/library/e4213hs1(v=vs.110).aspx

The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows:

  • The first operand is implicitly converted tobool. It is evaluated and all side effects are completed before continuing.

    第一个操作数自动转换成bool类型值。

  • If the first operand evaluates totrue(1), the second operand is evaluated.

  • If the first operand evaluates tofalse(0), the third operand is evaluated.

The result of the conditional operator is the result of whichever operand is evaluated — the second or the third. Only one of the last two operands is evaluated in a conditional expression.

Conditional expressions have right-to-left associativity(从右至左结合性). The first operand must be of integral or pointer type. The following rules apply to the second and third expressions:

  • If both expressions are of the same type, the result is of that type.

  • If both expressions are of arithmetic or enumeration types, the usual arithmetic conversions (covered inArithmetic Conversions) are performed to convert them to a common type. //如果两个表达式类型不相同,但是都是算术或者是枚举类型。那么算术转换将他们转换成共同类型。

  • If both expressions are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type. //指针类型转换

  • If both expressions are of reference types, reference conversions are performed to convert them to a common type. //引用类型转换

  • If both expressions are of type void, the common type is type void. //void 最低级的类型

  • If both expressions are of a given class type, the common type is that class type. //类类型的基本转换是他的基类的类型。

Any combinations of second and third operands not in the preceding list are illegal. The type of the result is the common type, and it is an l-value if both the second and third operands are of the same type and both are l-values.

通过上面的说明可以看到common_type是通过隐式类型转换来实现的。看来对一些基本概念的掌握还是不牢固啊。

分享到:
评论

相关推荐

    面向对象与C++试题.doc

    8、列出C++中两种用户自定义的数据类型: 、 。 9、构造函数的作用是 。 10、后置自增运算符“++”重载为类的成员函数(设类名为A)的形式为 。 三、阅读下面3个程序,写出程序运行时输出的结果:(共13分) 1、#...

    新手学习C++入门资料

    C++中还包括wchar_t数据类型,wchar_t也是字符类型,但是是那些宽度超过8位的数据类型。许多外文字符集所含的数目超过256个,char字符类型无法完全囊括。wchar_t数据类型一般为16位。 标准C++的iostream类库中包括...

    二级c未来教育题库破解及资源

    4.C表达式类型(赋值表达式,算术表达式,关系表达式,逻辑表达式,条件表达式,逗号表达式)和求值规则。  三、基本语句 1.表达式语句,空语句,复合语句。 2.输入输出函数的调用,正确输入数据并正确设计...

    Visual C++ 数据库系统开发完全手册.part2

    2.3 C++数据类型 2.3.1 基本数据类型 2.3.2 数组 2.3.3 指针 2.3.4 结构 2.3.5 枚举类型 2.4 控制结构 2.4.1 顺序结构 2.4.2 分支结构 2.4.3 循环结构 2.4.4 转移语句 2.5 函数 2.5.1 函数的定义与调用 2.5.2 默认...

    Visual C++ 数据库系统开发完全手册.part1

    2.3 C++数据类型 2.3.1 基本数据类型 2.3.2 数组 2.3.3 指针 2.3.4 结构 2.3.5 枚举类型 2.4 控制结构 2.4.1 顺序结构 2.4.2 分支结构 2.4.3 循环结构 2.4.4 转移语句 2.5 函数 2.5.1 函数的定义与调用 2.5.2 默认...

    09年二级C语言考试大纲

    4.C表达式类型(赋值表达式,算术表达式,关系表达式,逻辑表达式,条件表达式,逗号表达式)和求值规则。 三、基本语句 1.表达式语句,空语句,复合语句。 2.输入输出函数的调用,正确输入数据并正确设计...

    PHP基础教程 是一个比较有价值的PHP新手教程!

    2.1 首要条件 你首先必须要有一个正在工作着的支持PHP的web服务器。我假定在你的服务器上所有PHP文件的扩展名为.php3。 2.2 PHP的安装 生成一个名为test.php3的文件,含有以下内容: &lt;? phpinfo(); ?&gt; 然后在你的...

    java 面试题 总结

    Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。Int是java的原始数据类型,Integer是java为int提供的封装类。Java为每个原始类型提供了封装类。 原始类型封装类 booleanBoolean charCharacter byte...

    超级有影响力霸气的Java面试题大全文档

     Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。Int是java的原始数据类型,Integer是java为int提供的封装类。Java为每个原始类型提供了封装类。 原始类型 封装类 boolean Boolean char Character ...

    c#学习笔记.txt

    集合类型: 必须是 interface、class 或 struct。 必须包括返回类型的名为 GetEnumerator 的实例方法,例如 Enumerator(详见下文)。 Enumerator 类型(类或结构)必须包含: 一个名为 Current 的属性,它返回 ...

    Java 语言基础 —— 非常符合中国人习惯的Java基础教程手册

    然 new 运算符返回对一个对象的引用,但与 C、C++中的指针不同,对象的引用是指 向一个中间的数据结构,它存储有关数据类型的信息以及当前对象所在的堆的地址, 而对于对象所在的实际的内存地址是不可操作的,这就保证...

Global site tag (gtag.js) - Google Analytics