Topic: || and && with exceptions
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/01/13 Raw View
Christopher Eltschka writes:
> template<class T> T& operator||(T& t,const MyException& e)
> class OpenFailed: public MyException { /* ... */ }
> // (this is probably in an included header file)
> myfile=open(file) || OpenFailed(file);
> // No () needed around assignment, because operator||(t&, const e&)
> // returns t again.
This has the side-effect of constructing an instance of OpenFailed
every time a file is opened, no matter whether it is necessary or not.
If this constructor has important side-effects, this may be highly
undesirable.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1997/01/09 Raw View
Risto Lankinen wrote:
>
> Hi!
>
> Friedrich Knauss <fknauss@qualcomm.com> wrote in article
> <59mqrc$3v8@qualcomm.com>...
> > Try this:
> >
> > template <class E> inline bool Throw(E &exp) {throw exp; return false;}
> >
> > Since || has higher precedence than ||, you'd write your above expression
> as
> >
> > (FILE = open(file)) || Throw("Couldn't open file");
>
> This is extremely dangerous, because you don't know whether the programmer
> or a class library has an overloaded 'operator||(FILE,bool)' or a
> conversion
> from 'FILE' to a type that has an overloaded 'operator||()'.
>
> Why? Because only the built-in logical operators by-pass the evaluation of
> the subsequent arguments, if the first argument alone satisfies the
> condition.
> This is NOT the case with overloaded definitions - in the example above,
> both
> sides would be evaluated (i.e. a file opened and an exception thrown)
> before
> an overloaded 'operator||()' could even be called.
>
> terv: Risto
If you derive all thrown exceptions from a common class,
say MyException, you could define
template<class T> T& operator||(T& t,const MyException& e)
{
if(!t) throw e;
return t;
}
Then the example could be rewritten as:
class OpenFailed: public MyException { /* ... */ }
// (this is probably in an included header file)
myfile=open(file) || OpenFailed(file);
// No () needed around assignment, because operator||(t&, const e&)
// returns t again.
// Probably no other overloaded operator|| exists with MyException
// as second argument.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: fknauss@qualcomm.com (Friedrich Knauss)
Date: 1996/12/23 Raw View
In article <ord8w5m132.fsf_-_@dcc.unicamp.br>,
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
>Then I'd prefer to write:
>
>FILE = open(file) || throw "Cannot open file";
[...]
Try this:
template <class E> inline bool Throw(E &exp) {throw exp; return false;}
Since || has higher precedence than ||, you'd write your above expression as
(FILE = open(file)) || Throw("Couldn't open file");
You can also modify it to return a reference to exp if you want to
do the parameter evaluation order test. I'm not sure it'll always
work, since I don't think that there's anything in the standard that
mandates that parameters always be evaluated in the same order for
all calls to the same function.
I'm not sure at all that this is a good idea, and I think the existing
conditional sytax is clear enough, but what you want to do is possible
with the current language.
--
-- fritzz@qualcomm.com
-- Pain is temporary, glory is forever.
--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Risto Lankinen" <rlankine@us.oracle.com>
Date: 1996/12/26 Raw View
Hi!
Friedrich Knauss <fknauss@qualcomm.com> wrote in article
<59mqrc$3v8@qualcomm.com>...
> Try this:
>
> template <class E> inline bool Throw(E &exp) {throw exp; return false;}
>
> Since || has higher precedence than ||, you'd write your above expression
as
>
> (FILE = open(file)) || Throw("Couldn't open file");
This is extremely dangerous, because you don't know whether the programmer
or a class library has an overloaded 'operator||(FILE,bool)' or a
conversion
from 'FILE' to a type that has an overloaded 'operator||()'.
Why? Because only the built-in logical operators by-pass the evaluation of
the subsequent arguments, if the first argument alone satisfies the
condition.
This is NOT the case with overloaded definitions - in the example above,
both
sides would be evaluated (i.e. a file opened and an exception thrown)
before
an overloaded 'operator||()' could even be called.
terv: Risto
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]