Files
2025-10-25 03:02:53 +03:00

125 lines
5.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[expr.rel]
# 7 Expressions [[expr]](./#expr)
## 7.6 Compound expressions [[expr.compound]](expr.compound#expr.rel)
### 7.6.9 Relational operators [expr.rel]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7404)
The relational operators group left-to-right[.](#1.sentence-1)
[*Example [1](#example-1)*:
a<b<c means (a<b)<c and not(a<b)&&(b<c)[.](#1.sentence-2)
— *end example*]
[relational-expression:](#nt:relational-expression "7.6.9Relational operators[expr.rel]")
[*compare-expression*](expr.spaceship#nt:compare-expression "7.6.8Three-way comparison operator[expr.spaceship]")
[*relational-expression*](#nt:relational-expression "7.6.9Relational operators[expr.rel]") < [*compare-expression*](expr.spaceship#nt:compare-expression "7.6.8Three-way comparison operator[expr.spaceship]")
[*relational-expression*](#nt:relational-expression "7.6.9Relational operators[expr.rel]") > [*compare-expression*](expr.spaceship#nt:compare-expression "7.6.8Three-way comparison operator[expr.spaceship]")
[*relational-expression*](#nt:relational-expression "7.6.9Relational operators[expr.rel]") <= [*compare-expression*](expr.spaceship#nt:compare-expression "7.6.8Three-way comparison operator[expr.spaceship]")
[*relational-expression*](#nt:relational-expression "7.6.9Relational operators[expr.rel]") >= [*compare-expression*](expr.spaceship#nt:compare-expression "7.6.8Three-way comparison operator[expr.spaceship]")
The
lvalue-to-rvalue ([[conv.lval]](conv.lval "7.3.2Lvalue-to-rvalue conversion"))
and function-to-pointer ([[conv.func]](conv.func "7.3.4Function-to-pointer conversion"))
standard conversions are performed on the operands[.](#1.sentence-3)
If one of the operands is a pointer, the
array-to-pointer conversion ([[conv.array]](conv.array "7.3.3Array-to-pointer conversion")) is performed on the other operand[.](#1.sentence-4)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7435)
The converted operands shall have arithmetic, enumeration, or pointer type[.](#2.sentence-1)
The
operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all
yield false or true[.](#2.sentence-2)
The type of the result isbool[.](#2.sentence-3)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7443)
The usual arithmetic conversions ([[expr.arith.conv]](expr.arith.conv "7.4Usual arithmetic conversions")) are performed on operands of arithmetic
or enumeration type[.](#3.sentence-1)
If both converted operands are pointers,
pointer conversions ([[conv.ptr]](conv.ptr "7.3.12Pointer conversions")),
function pointer conversions ([[conv.fctptr]](conv.fctptr "7.3.14Function pointer conversions")), and
qualification conversions ([[conv.qual]](conv.qual "7.3.6Qualification conversions"))
are performed to bring
them to their [composite pointer type](expr.type#def:composite_pointer_type "7.2.2Type[expr.type]")[.](#3.sentence-2)
After conversions, the operands shall have the same type[.](#3.sentence-3)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7453)
The result of comparing unequal pointers to objects[65](#footnote-65 "As specified in [basic.compound], an object that is not an array element is considered to belong to a single-element array for this purpose and a pointer past the last element of an array of n elements is considered to be equivalent to a pointer to a hypothetical array element n for this purpose.") is defined in terms of a partial order consistent with the following rules:
- [(4.1)](#4.1)
If two pointers point to different elements of the same array, or to
subobjects thereof, the pointer to the element with the higher subscript
is required to compare greater[.](#4.1.sentence-1)
- [(4.2)](#4.2)
If two pointers point to different non-static data members of the same
object, or to subobjects of such members, recursively,
the pointer to the later declared member is required to compare greater provided
neither member is a subobject of zero size
and their class is not a union[.](#4.2.sentence-1)
- [(4.3)](#4.3)
Otherwise, neither pointer is required to compare greater than the other[.](#4.3.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7479)
If two operands p and q compare equal ([[expr.eq]](expr.eq "7.6.10Equality operators")),p<=q and p>=q both yield true and p<q andp>q both yield false[.](#5.sentence-1)
Otherwise, if a pointer to object p compares greater than a pointer q, p>=q, p>q,q<=p, and q<p all yield true and p<=q,p<q, q>=p, and q>p all yield false[.](#5.sentence-2)
Otherwise, the result of each of the operators is unspecified[.](#5.sentence-3)
[*Note [1](#note-1)*:
A relational operator applied
to unequal function pointers
yields an unspecified result[.](#5.sentence-4)
A pointer value of type “pointer to cv void”
can point to an object ([[basic.compound]](basic.compound "6.9.4Compound types"))[.](#5.sentence-5)
— *end note*]
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7495)
If both operands (after conversions) are of arithmetic or enumeration type, each
of the operators shall yield true if the specified relationship is true
and false if it is false[.](#6.sentence-1)
[65)](#footnote-65)[65)](#footnoteref-65)
As specified in [[basic.compound]](basic.compound "6.9.4Compound types"),
an object that is not an array element
is considered to belong to a
single-element array for this purpose and
a pointer past the last element of an array of n elements
is considered to be equivalent to a pointer to a hypothetical array elementn for this purpose[.](#footnote-65.sentence-1)