是操作"假<真QUOT;明确界定? [英] Is the operation "false < true" well defined?

查看:156
本文介绍了是操作"假<真QUOT;明确界定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问C ++标准定义:


  1. 布尔参数的小于操作者的存在,如果是这样,

  2. 4个参数排列的结果?

在换言之,是从以下的操作由规范定义的结果

 假LT;假
假<真正
真<假
真<真正

在我的设置(Centos的7,GCC 4.8.2)中,code以下吐出来,我期望是什么(因为重新presenting假为0,真正的为1的C'S史):

 假LT;假=假
假<真正=真
真<假=假
真<真=假

虽然我是pretty相信大多数(所有?)编译器将给出相同的输出,这是由C ++规范的立法?或者是不解的,但符合规范的编译器有权决定,真正低于假的?

 的#include<&iostream的GT;为const char * S(布尔一)
{
  返回(一个真:假);
}无效测试(布尔一,布尔B)
{
  性病::法院LT&;<秒(一)及下;&下; <<< S(B)<< =&所述;&下; S(A< B)<<的std :: ENDL;
}INT主(INT ARGC,CHAR *的argv [])
{
  测试(假的,假的);
  测试(假,真);
  测试(真,假);
  测试(真实的,真实的);
  返回0;
}


解决方案

TL; DR:

的操作是按照C ++标准草案明确界定。

详细信息

我们可以看到,通过转到草案C ++标准部分 5.9 关系运算符的它说(的重点煤矿前进的):


  

操作数应具有算术,枚举,或指针的类型或类型的std :: nullptr_t。运营商
  < (小于),>(大于),LT =(小于或等于),和> =(大于或等于)所有产
  或真或假。结果类型是布尔


和布尔变量是arithematic类型从3.9.1基本类型


  

类型的布尔,焦炭,char16_t,char32_t,wchar_t的和符号和无符号整数类型统称
  称为积分类型。



  

整型和浮点型统称算术
  类型。


真正 2.14.6 布尔文字:

 布尔文字:
    假
    真正

让我们回到第 5.9 来看看关系运算符的机制。此外,它说:


  

常见的算术转换算术或枚举类型的操作数执行。


通常的算术转换的将在第 5 它说:


  

另外,积分促销活动(4.5),应在两个操作数执行


和部分 4.5 说:


  

一个布尔型的prvalue可以被转换成int类型的prvalue,以虚假成为零和真
  成为一体。


等除权pressions:

 假LT;假
假<真正
真<假
真<真正

用这些规则变成了:

  0℃; 0
0℃; 1
1< 0
1< 1

Does the C++ specification define:

  1. the existence of the 'less than' operator for boolean parameters, and if so,
  2. the result of the 4 parameter permutations?

In other words, are the results from the following operations defined by the specification?

false < false
false < true
true < false
true < true

On my setup (Centos 7, gcc 4.8.2) , the code below spits out what I'd expect (given C's history of representing false as 0 and true as 1):

false < false = false
false < true = true
true < false = false
true < true = false

Whilst I'm pretty sure most (all?) compilers will give the same output, is this legislated by the C++ specification? Or is an obfuscating, but specification-compliant compiler allowed to decide that true is less than false?

#include <iostream>

const char * s(bool a)
{
  return (a ? "true" : "false");
}

void test(bool a, bool b)
{
  std::cout << s(a) << " < " << s(b) << " = " << s(a < b) << std::endl;
}

int main(int argc, char* argv[])
{
  test(false, false);
  test(false, true);
  test(true, false);
  test(true, true);
  return 0;
}

解决方案

TL;DR:

The operations are well defined according to the draft C++ standard.

Details

We can see that by going to the draft C++ standard section 5.9 Relational operators which says (emphasis mine going forward):

The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t. The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool

and bools are arithematic types from 3.9.1 Fundamental types

Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types.

and

Integral and floating types are collectively called arithmetic types.

and true and false are boolean literals from 2.14.6 Boolean literals:

boolean-literal:
    false
    true

Going back to section 5.9 to see the mechanics of the relational operators further, it says:

The usual arithmetic conversions are performed on operands of arithmetic or enumeration type.

the usual arithmetic conversions are covered in section 5 which says:

Otherwise, the integral promotions (4.5) shall be performed on both operands

and section 4.5 says:

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

and so the expressions:

false < false
false < true
true < false
true < true

using these rules become:

0 < 0
0 < 1
1 < 0
1 < 1

这篇关于是操作&QUOT;假&LT;真QUOT;明确界定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆