操作是否为“假<"?真的"定义明确? [英] Is the operation "false < true" well defined?

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

问题描述

C++ 规范是否定义:

Does the C++ specification define:

  1. 布尔参数是否存在小于"运算符,如果存在,
  2. 4 个参数排列的结果?

换句话说,规范定义了以下操作的结果吗?

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

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

在我的设置 (Centos 7, gcc 4.8.2) 中,下面的代码给出了我的预期(鉴于 C 将 false 表示为 0 并将 true 表示为 1 的历史):

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

虽然我很确定大多数(所有?)编译器都会给出相同的输出,但这是由 C++ 规范规定的吗?还是允许混淆但符合规范的编译器决定真小于假?

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:

这些操作根据 C++ 标准草案进行了很好的定义.

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

详情

我们可以通过转到 C++ 标准草案 部分 5.9 关系运算符 说(强调我的未来):

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

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

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

和 bool 是 3.9.1 基本类型中的算术类型

and bools are arithematic types from 3.9.1 Fundamental types

类型bool、char、char16_t、char32_t、wchar_t,有符号和无符号整数类型统称为称为整数类型.

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

整数和浮点类型统称为算术类型.

Integral and floating types are collectively called arithmetic types.

truefalse 是来自 2.14.6 的布尔文字:

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

boolean-literal:
    false
    true

回到 5.9 部分,进一步了解关系运算符的机制,它说:

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.

常用的算术转换5 部分中有介绍,其中说:

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

否则,应在两个操作数上执行积分提升 (4.5)

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

4.5 节说:

bool 类型的纯右值可以转换为 int 类型的纯右值,false 变为 0 和 true合而为一.

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

等等表达式:

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

使用这些规则变成:

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

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

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