J
jacob navia
C++ introduced an interesting feature (among others): operator overloading.
The idea is to build a mechanism for the user defining its own number types and the operations to be
done with them.
I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350
bit floats.
Yes, 350. We have a lot of fast machines now. Why not use them? The extra-precision is useful in
some algorithms.
This extension to C is transparent, since it doesn't change anything in the basics of the language.
The syntax used is:
TYPE operator+(TYPE a,TYPE b)
{
}
When in your program you use
TYPE a,b;
...
a+b;
Means that you call the operator function with those two numbers. This is similar to C++, but since
there are no classes (C is not object oriented) everything is quite simple.
Note that this is an extension compatible with C and it is important to know that the C standard
doesn't forbid extensions. They should not introduce new keywords though.
In my implementation it means that
int operator = 6;
still works as it should. No new keywords.
The operator symbol is recognized if (and only if):
1) The level is global, i.e. not within a function definition.
2) It is followed by one of the defined operator symbols (+, -, etc)
3) This must be followed by a normal function definition. The name of this function is the sequence
of types in the argument list, the "signature" in technical terms.
Is this useful?
Yes, for numbers it is ideal. It is less so, when the data types aren't quantities but other
relationships or data. Take, for instance
String a,b,c;
...
c = a+b;
This means that a+b is different from b+a. Not very clear.
Operator overloding *can* be useful, specially for letting the users build new kinds of numbers,
that fulfill special needs.
What do you think?
Voice your opinion. In any case Lcc-win32 is available at:
http://www.cs.virginia.edu/~lcc-win32
The idea is to build a mechanism for the user defining its own number types and the operations to be
done with them.
I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350
bit floats.
Yes, 350. We have a lot of fast machines now. Why not use them? The extra-precision is useful in
some algorithms.
This extension to C is transparent, since it doesn't change anything in the basics of the language.
The syntax used is:
TYPE operator+(TYPE a,TYPE b)
{
}
When in your program you use
TYPE a,b;
...
a+b;
Means that you call the operator function with those two numbers. This is similar to C++, but since
there are no classes (C is not object oriented) everything is quite simple.
Note that this is an extension compatible with C and it is important to know that the C standard
doesn't forbid extensions. They should not introduce new keywords though.
In my implementation it means that
int operator = 6;
still works as it should. No new keywords.
The operator symbol is recognized if (and only if):
1) The level is global, i.e. not within a function definition.
2) It is followed by one of the defined operator symbols (+, -, etc)
3) This must be followed by a normal function definition. The name of this function is the sequence
of types in the argument list, the "signature" in technical terms.
Is this useful?
Yes, for numbers it is ideal. It is less so, when the data types aren't quantities but other
relationships or data. Take, for instance
String a,b,c;
...
c = a+b;
This means that a+b is different from b+a. Not very clear.
Operator overloding *can* be useful, specially for letting the users build new kinds of numbers,
that fulfill special needs.
What do you think?
Voice your opinion. In any case Lcc-win32 is available at:
http://www.cs.virginia.edu/~lcc-win32