是操作&QUOT;假&LT;真QUOT;明确界定? [英] Is the operation "false < true" well defined?
问题描述
请问C ++标准定义:
- 布尔参数的小于操作者的存在,如果是这样,
- 4个参数排列的结果?
在换言之,是从以下的操作由规范定义的结果
假LT;假
假&LT;真正
真&LT;假
真&LT;真正
在我的设置(Centos的7,GCC 4.8.2)中,code以下吐出来,我期望是什么(因为重新presenting假为0,真正的为1的C'S史):
假LT;假=假
假&LT;真正=真
真&LT;假=假
真&LT;真=假
虽然我是pretty相信大多数(所有?)编译器将给出相同的输出,这是由C ++规范的立法?或者是不解的,但符合规范的编译器有权决定,真正低于假的?
的#include&LT;&iostream的GT;为const char * S(布尔一)
{
返回(一个真:假);
}无效测试(布尔一,布尔B)
{
性病::法院LT&;&LT;秒(一)及下;&下; &LT;&LT;&LT; S(B)&LT;&LT; =&所述;&下; S(A&LT; B)&LT;&LT;的std :: ENDL;
}INT主(INT ARGC,CHAR *的argv [])
{
测试(假的,假的);
测试(假,真);
测试(真,假);
测试(真实的,真实的);
返回0;
}
TL; DR:
的操作是按照C ++标准草案明确界定。
详细信息
我们可以看到,通过转到草案C ++标准一>部分 5.9
的关系运算符的它说(的重点煤矿前进的):
在操作数应具有算术,枚举,或指针的类型或类型的std :: nullptr_t。运营商
&LT; (小于),>(大于),LT =(小于或等于),和> =(大于或等于)所有产
或真或假。结果类型是布尔
块引用>和布尔变量是arithematic类型从3.9.1基本类型
类型的布尔,焦炭,char16_t,char32_t,wchar_t的和符号和无符号整数类型统称
称为积分类型。
块引用>和
整型和浮点型统称算术
类型。
块引用>和
真正
和假
从2.14.6 $ C布尔文字$ C>布尔文字:
布尔文字:
假
真正让我们回到第
5.9
来看看关系运算符的机制。此外,它说:
常见的算术转换算术或枚举类型的操作数执行。
块引用>的通常的算术转换的将在第
5
它说:
另外,积分促销活动(4.5),应在两个操作数执行
块引用>和部分
4.5
说:
一个布尔型的prvalue可以被转换成int类型的prvalue,以虚假成为零和真
成为一体。
块引用>等除权pressions:
假LT;假
假&LT;真正
真&LT;假
真&LT;真正用这些规则变成了:
0℃; 0
0℃; 1
1&LT; 0
1&LT; 1Does the C++ specification define:
- the existence of the 'less than' operator for boolean parameters, and if so,
- 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
andfalse
are boolean literals from2.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屋!