C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: can i overload operator< to get (a < b < c)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Siemel Naran
Guest





PostPosted: Thu Jun 26, 2003 2:22 pm    Post subject: Re: can i overload operator< to get (a < b < c) Reply with quote



"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





PostPosted: Thu Jun 26, 2003 6:56 pm    Post subject: Re: can i overload operator< to get (a < b < c) Reply with quote



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





PostPosted: Fri Jun 27, 2003 11:01 am    Post subject: Re: can i overload operator< to get (a < b < c) Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.