F
Flash Gordon
jacob said:Richard Heathfield a écrit :
Exactly. That is why you would use operator overloading in computers
that have the MMX instruction set (PCs) and not use any operator
overloading for those operations in computers that do NOT have that
feature.
Compare this solution with other solutions where the language would add
a special |+ operation (or similar) to the language itself.
All implementations would have to use it.
The advantage of operator overloading here is PRECISELY that you do it
where it is needed and nowhere else!
If I write code depending on the |+ operator then I need it implemented
on all the systems I need it ported to. It does not matter to me whether
it is implemented as an MMX instruction or a function call. You don't
need operator overloading for that, you need it added to the base language.
The language is made extensible without adding any specific extension.
The reserved namespace and stating in the standard that extensions are
allowed does that. Hell, since |+ is currently an error you can add it
as an extension to your compiler if you want, as long as it produces a
diagnostic when invoked in standard conforming mode! No need for
operator overloading to allow you to do that.
B)
This feature is useful when applied to special forms of arrays. Suppose:
typedef struct _String {
size_t length;
char *data;
} String;
If you want to maintain the natural syntax
String s;
...
s[2] = 'e';
it is good to be able to overload the [].
That's arguable. Some might consider it good. Others might consider it
unnecessarily confusing. [] suggests that s is an array, which it
clearly is not.
Excuse me, it IS an array, but indexed with an size_t offset from the
beginning that is all.
It's a structure containing an array.
Why is this:
SomeIntType_t a,b=7,c=6;
a = b*c;
more *difficult* to read than
SomeIntType_t a,b,c;
intToSomeIntType(&b,7);
intToSomeIntType(&c,6);
SomeIntType_Add(&c,b,a);
?????????
Well, your suggestion here seems to have use * for addition... ;-)
Anyway, I would have done
SomeIntType_t a;
SomeIntType_t b = makeSomeTime(7);
SomeIntType_t c = makeSomeTime(6);
c = SomeTypeMult(b,c);
Unless the operation is very complex, in which case it might be worth
people knowing that rather than thinking, oh, that's just a multiplication.
Operator overloading may well be useful. So feel free to propose it to
the standard committee. However, that is not this group which discusses
the existing language.