Topic: Overloading || and && (Was: Sequence Points and comma-Operator)


Author: "Ken Hagan" <K.Hagan@thermoteknix.co.uk>
Date: Mon, 5 Mar 2001 16:28:24 GMT
Raw View
In the thread "Sequence Points and comma-Operator", I said
>
> Overloads of comma, && and !! are (obviously) things that a compiler
> can identify and flag. The use of a conventional function is a simple
> and suitable replacement in most cases. Almost everyone agrees they
> are brain damage. Surely we can deprecate them in a few years time.

"Andrei Alexandrescu" <andrewalex@hotmail.com> replied...
>
> Expression templates do overload && and || and I don't find that that bad.
>

They do? I presume this is only in those circumstances where they
don't need to preserve the short-circuit property. That is, "a||b"
can be captured by such a template, but "a||++b" wouldn't be.

Even in this case, I'm surprised, since I'd thought of ETs as a
convenient way of rolling together things like matrix calculations
where there are significant optimisations to be gained by avoiding
expensive temporaries, or by doing things in parallel (loop fusion).

If the && and || that the ET captures are the native operators, then
I'm surprised that there are any savings to be found. I suppose that
"a+b||c+d" may contain fusable loops, but what is the type of a+b?
If it is boolean, or a UDT with a boolean conversion, we can use the
bitwise operators instead of the logical ones, with no suggestion of
a sequence point, lost or otherwise.

If the && and || are overloads for some type, then I re-iterate my
general reluctance to overload them in the first place. The loss of
the sequence point (with no visual cues for a code reviewer) just
looks like an accident waiting to happen.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: gt5163b@prism.gatech.edu (Brian McNamara!)
Date: Wed, 7 Mar 2001 22:55:12 GMT
Raw View
"Ken Hagan" <K.Hagan@thermoteknix.co.uk> once said:
>"Andrei Alexandrescu" <andrewalex@hotmail.com> replied...
>> Expression templates do overload && and || and I don't find that that bad.
>
>They do? I presume this is only in those circumstances where they
>don't need to preserve the short-circuit property. That is, "a||b"
>can be captured by such a template, but "a||++b" wouldn't be.

No, they can preserve the short-circuit properties with respect to the
expressions they're templatizing.  Keep in mind that most expression
templates don't evaluate anything until later.

As an example, using the lambda library ( http://lambda.cs.utu.fi/ ),
where we want to apply the expression
   f(x) && g(x)
to each element of a boolean array:

   #include <iostream>
   #include <algorithm>
   #include "ll/ll.hpp"

   using namespace std;

   bool f(bool x) {
      cout << "f" << endl;
      return x;
   }

   bool g(bool x) {
      cout << "g" << endl;
      return x;
   }

   int main() {
      bool a[] = { true, false };
      for_each( a, a+2, bind(&f, free1) && bind(&g, free1) );
   }

The output is
   f
   g
   f
which shows that the && is still short-circuit with respect to the ET
evaluation.  (Of course, C++ evaluates it strictly, but this evaluation
has no effects, as the ET delays all effects.)

--
 Brian M. McNamara   lorgon@acm.org  :  I am a parsing fool!
   ** Reduce - Reuse - Recycle **    :  (Where's my medication? ;) )

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Ken Hagan" <K.Hagan@thermoteknix.co.uk>
Date: Thu, 8 Mar 2001 12:57:50 GMT
Raw View
"Brian McNamara!" <gt5163b@prism.gatech.edu> wrote...
>
> No, they can preserve the short-circuit properties with respect to the
> expressions they're templatizing.  Keep in mind that most expression
> templates don't evaluate anything until later.
>
> As an example, using the lambda library ( http://lambda.cs.utu.fi/ ),
> where we want to apply the expression
>    f(x) && g(x)
> to each element of a boolean array:

[code snipped]

Umm, yes, I'd forgotten about lambdas. Since they preserve the sequence
point, I don't mind them, and since some sort of lambda is a very nice
thing to have, I suppose its time to change horses. Overloading &&, ||
and "," is a great feature and must never be deprecated. In my original
post, for "brain damage" please read "inspired foresight".

(D&E says comma overloads only made it into the language by mistake.
I wish I could make mistakes like that.)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]