 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Siemel Naran Guest
|
Posted: Thu Jun 26, 2003 2:22 pm Post subject: Re: can i overload operator< to get (a < b < c) |
|
|
"Geir Bjarte Terum" <clcppm-poster (AT) this (DOT) is.invalid> wrote
| Quote: | Yes it is possible to overload the operator < for any type including
built
in types. But one must be willing to add a statement controller.
|
Really? I thought in overloaded operator<(T,U) either T or U or both
should
be class types -- in other words, not builtin types.
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Geir Bjarte Terum Guest
|
Posted: Thu Jun 26, 2003 6:56 pm Post subject: Re: can i overload operator< to get (a < b < c) |
|
|
Geir Bjarte Terum wrote:
| Quote: | [snip]
#define IF(expr) if (eval << expr)
#define WHILE(expr) while (eval << expr)
#define EVAL(expr) (eval << expr)
Now you can write:
IF (a < b < c) { }
WHILE (a < b < c) { }
cout << EVAL(a < b < c) << endl;
|
James Kanze wrote:
(ref. news:d6652001.0306240329.50ce627f (AT) posting (DOT) google.com)
| Quote: |
Since macros aren't evaluated recursively, you can even write:
#define if( expr ) if ( eval << expr )
etc.
It's interesting to note that with this, something like:
if ( condition() && a < b < c ) ...
and
if ( a < b < c && condition() ) ...
could give different results.
This sounds like a good candidate for the obfuscated C++ contest.
|
Yes it is generally a bad idea to remove the information
contained in using upper letters for the IF macro etc.
And it is especially a bad idea when we have a case as
you describe in your example. A such declaration is
not only a good candidate for the obfuscated C++
contest, but is also potentially fatal as long as the
macro have special conditions of usage attached,
as your example suggest.
---
Geir Bjarte Terum
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Geir Bjarte Terum Guest
|
Posted: Fri Jun 27, 2003 11:01 am Post subject: Re: can i overload operator< to get (a < b < c) |
|
|
Geir Bjarte Terumwrote:
| Quote: | Yes it is possible to overload the operator < for any type including
built in types. But one must be willing to add a statement controller.
[ref. news:wPqJa.1515$os2.21179 (AT) news2 (DOT) e.nsc.no]
|
Siemel Naran wrote:
| Quote: | Really? I thought in overloaded operator<(T,U) either T or U
or both should be class types -- in other words, not builtin types.
|
In operator overloading at least one of the operator operands
must be a UDT. By using a statement controller, or more
presise - an expression evaluator, the left operand is a UDT
and which enable the programmer to get in control - even
for builtin types.
Example of overloading an int operator using an expression
evaluator:
class int_evaluator
{
int value;
public:
explicit int_evaluator(int value) : value(value) { }
bool operator < (int rhs) const
{
return value < rhs;
}
// ... other overloaded operators ...
};
struct evaluator
{
int_evaluator operator << (int rhs)
{
return int_evaluator(rhs);
}
};
evaluator eval;
#define IF(expr) if (eval << expr)
---
Geir Bjarte Terum
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|