C+++: User-Defined Operator Symbols in C++


Chainable Comparison Operators



Download 32,45 Kb.
Pdf ko'rish
bet5/7
Sana24.05.2023
Hajmi32,45 Kb.
#943285
1   2   3   4   5   6   7
Bog'liq
03 Heinlein

Chainable Comparison Operators
Comparison operators are another source of potential misinterpretations, at least for
novice programmers. While the C++ expression
a < b
corresponds exactly to the mathe-
matical term b, the meaning of the expression
a < b < c
is quite different from its
mathematical counterpart c, the latter meaning b and b c. The former is
actually interpreted as
(a < b) < c
, which compares the Boolean-valued result of com-
paring
a
and
b
with
c
. In many programming languages, this will lead to a compile time
error since Boolean values and numbers cannot be compared to each other. In C++, how-
ev er, the Boolean values
true
and
false
are implicitly converted to the integer values
1
and
0
, respectively, when necessary causing the expression
a < b < c
to be actually well-
defined, but probably not producing the desired result.
Because “chained” comparisons such as or 0

are occasionally useful
and more convenient than their logical expansions (such as 0

i and i n), one might
want to define corresponding operators in a programming language. Similar to the
avg


operator above, such operators must not only return a Boolean value representing the re-
sult of the current comparison, but also the value of their right operand which might be
needed as the left operand of the following operator, too. This can again be achieved by
introducing an appropriate auxiliary structure:
template
struct Cmp {
bool res; T val;
Cmp (bool r, T v) : res(r), val(v) {}
};
template
bool operator... (Cmp c) {
return c.res;
}
new operator &< stronger = weaker ||;
template
Cmp operator&< (T x, T y) {
return Cmp(x < y, y);
}
template
Cmp operator&< (Cmp c, T z) {
return Cmp(c.res && c.val < z, z);
}
// Likewise for operators
&<= &> &>= &== &!=
Now, an expression such as
a &< b &< c
is indeed equivalent to
a < b && b < c
.

Download 32,45 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish