Topic: exception specifications and overriding


Author: "Alfred Kellner" <alfkellner@magnet.at>
Date: 1998/10/10
Raw View
Valentin Bonnard <bonnardv@pratique.fr> wrote
> Does anyone think that the following isn't valid ?
>
> struct X {};
> struct Y : X {};
>
> struct A {
>     virtual void foo() throw (X);
> };
> struct A : B {
>     void foo() throw (Y);
> };
>
> It may be a silly question, but I ask it because the french
> commitee (except me) is convinced that it's ill-formed code.
>
I vote:) valid.
<qoute> 15.4 Exception specifications
 (3) If a virtual function has an exception-specification, all
 declarations, including the definition, of any function that
 overrides that virtual function in any derived class shall only
 allow exceptions that are allowed by the exception-specification
 of the base class virtual function.
 ......
 (7) If a class X is in the type-id-list of the exception-specification
 of a function, that function is said to allow exception objects of
 class X or any class publicly and unambiguously derived from X.
</qoute>
Thus B::foo() throw (Y); does not enlarge the range of
allowed exceptions A::foo() throw (X); specifies
since Y is publicly and unambiguously derived from X.

Can't see any reason why this could be ill-formed code.
 --ALfred


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/10/10
Raw View
Kendall Beaman wrote:
>
> Valentin Bonnard wrote:
>
> > struct X {};
> > struct Y : X {};
> >
> > struct A {
> >     virtual void foo() throw (X);
> > };
> > struct A : B {

Hum

B : A

> >     void foo() throw (Y);
> > };

> Not sure about the throw exceptions but I believe that overloading the
> virtual foo function in the derived object is not good

I have overriden A::foo () with B::foo (). What do you mean ?

> and can lead to
> problems and confusions.

If you ignore exception specifications, this example is the
simpliest example of how to declare a virtual function.
The point is the exception specification.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: abrahams@spam.motu.com (David Abrahams)
Date: 1998/10/11
Raw View
On 09 Oct 98 06:13:51 GMT, Valentin Bonnard <bonnardv@pratique.fr>
wrote:

>Does anyone think that the following isn't valid ?
>
>struct X {};
>struct Y : X {};
>
>struct A {
>    virtual void foo() throw (X);
>};
>struct A : B {
>    void foo() throw (Y);
>};
>
>It may be a silly question, but I ask it because the french
>commitee (except me) is convinced that it's ill-formed code.

It looks well-formed to me. On what basis do they contend that it is
not?
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Kendall Beaman <beaman@mindspring.com>
Date: 1998/10/11
Raw View
Valentin Bonnard wrote:

> Kendall Beaman wrote:
> >
> > Valentin Bonnard wrote:
> [snip]

> If you ignore exception specifications, this example is the
> simpliest example of how to declare a virtual function.
> The point is the exception specification.

Well, I was waiting for some C++ master to correct me or clarify but now that
I'm a bit more awake :) I'll do it. The rule I mis-remembered was talking
about overriding a non-virtual inherited function which wasn't the case. This
didn't have anything really to do with the question so oh well.

As for the question itself I would think its safer to keep the same exception
specification as the virtual function.

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

If you ask for nothing then nothing is probably what you'll get. If you ask
for everything then chances are you may get something.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/10/11
Raw View
Jim Barry wrote:
>
> Valentin Bonnard wrote in message <361CF054.6815@pratique.fr>...
> >Does anyone think that the following isn't valid ?
> >
> >struct X {};
> >struct Y : X {};
> >
> >struct A {
> >    virtual void foo() throw (X);
> >};
> >struct A : B {
>
> "struct B : A", shurely?

Of course. I read my code several times without seeing that
typo.

> >    void foo() throw (Y);
> >};
> >
> >It may be a silly question, but I ask it because the french
> >commitee (except me) is convinced that it's ill-formed code.
>
> I think this is covered in Stroustrup 3e, 14.6.1: "A virtual function
> may be overriden only by a function that has an
> exception-specification at least as restrictive as its own (explicit
> or implicit) exception-specification. This rule is really only common
> sense.

Yes !

> If a derived class threw an exception that the original
> function didn't advertise, a caller couldn't be expected to catch it."

The interpretation of most members (not me) of the french
commitee was that exception specifications are contravariants:

struct X2 {};

A::foo () throw (X, X2)

B::foo () throw (X)

the throw (X) specification is smaller (more restrictive)
than throw (X, X2). In other words, the interpretation was
that derived throw-spec must have less type names than
base throw-spec. So the first example above is illegal
(with this strange interpretation) because 'throw (Y)' is
<< larger >> than 'throw (X)', not smaller (Y has more
data members than X).

I am happy to see that no one here buy this non-sens.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Anatoli <REMOVETHIS.anatoli@ptc.com>
Date: 1998/10/11
Raw View
Valentin Bonnard wrote:
[snip]
> the throw (X) specification is smaller (more restrictive)
> than throw (X, X2). In other words, the interpretation was
> that derived throw-spec must have less type names than
> base throw-spec. So the first example above is illegal
> (with this strange interpretation) because 'throw (Y)' is
> << larger >> than 'throw (X)', not smaller (Y has more
> data members than X).
>
> I am happy to see that no one here buy this non-sens.
>

> The interpretation of most members (not me) of the french
> commitee was that exception specifications are contravariants:

No, they are covariants, exactly like return types of functions.

> struct X2 {};
>
> A::foo () throw (X, X2)
>
> B::foo () throw (X)


A function with throw-spec 'throw(X)' can throw any X *including*
any Y (because Y is-a X).  A function with throw-spec 'throw(Y)'
can throw any Y whic is a subset of what the first function can throw.
This set is smaller, not larger.
--
Regards
Anatoli (anatoli at ptc dot com) -- opinions aren't
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/10/12
Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:

>> If a derived class threw an exception that the original
>> function didn't advertise, a caller couldn't be expected to catch it."

> The interpretation of most members (not me) of the french
[...]
> was that derived throw-spec must have less type names than base
> throw-spec. So the first example above is illegal (with this strange
> interpretation) because 'throw (Y)' is << larger >> than 'throw
> (X)', not smaller (Y has more data members than X).

Just argue that Y:X is in fact more restricted than X.  It is saying
that it can't throw any X, only one particular type of X, namely, Y.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:oliva@gnu.org mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/10/09
Raw View
Does anyone think that the following isn't valid ?

struct X {};
struct Y : X {};

struct A {
    virtual void foo() throw (X);
};
struct A : B {
    void foo() throw (Y);
};

It may be a silly question, but I ask it because the french
commitee (except me) is convinced that it's ill-formed code.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/10/09
Raw View
In article <361CF054.6815@pratique.fr>,
  Valentin Bonnard <bonnardv@pratique.fr> wrote:
> Does anyone think that the following isn't valid ?
>
> struct X {};
> struct Y : X {};
>
> struct A {
>     virtual void foo() throw (X);
> };
> struct A : B {
         ^^^^^      Can we assume you meant "struct B : A" ??
>     void foo() throw (Y);
> };
>
> It may be a silly question, but I ask it because the french
> commitee (except me) is convinced that it's ill-formed code.

So A::foo() throws X, but B::foo() throws Y?
If this isn't legal, it should be...

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Kendall Beaman <beaman@mindspring.com>
Date: 1998/10/09
Raw View
Valentin Bonnard wrote:

> Does anyone think that the following isn't valid ?
>
> struct X {};
> struct Y : X {};
>
> struct A {
>     virtual void foo() throw (X);
> };
> struct A : B {
>     void foo() throw (Y);
> };
>
> It may be a silly question, but I ask it because the french
> commitee (except me) is convinced that it's ill-formed code.
>

Not sure about the throw exceptions but I believe that overloading the
virtual foo function in the derived object is not good and can lead to
problems and confusions. I believe Scott Meyers warns about this thing in his
book(s).

I'm guessing that the second struct was supposed to be struct B : A ?

Anyone else? I don't have Effective C++ or More Effective C++ in front of me
but I seem to remember that one of the rules was not to do this?



--
-=-=-=-=-=-=-=-=-=-=-Web Site: http://www2.andrews.edu/~beaman-=-=-==-=-=-=-
If you ask for nothing then nothing is probably what you'll get. If you ask
for everything then chances are you may get something.
                                  A Very Special Friend of Mine
-=-=-=-=-=-=-=-=-=-=-Web Site: http://www2.andrews.edu/~beaman-=-=-==-=-=-=-




[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jim Barry" <jim.barry@bigfoot.com>
Date: 1998/10/09
Raw View
Valentin Bonnard wrote in message <361CF054.6815@pratique.fr>...
>Does anyone think that the following isn't valid ?
>
>struct X {};
>struct Y : X {};
>
>struct A {
>    virtual void foo() throw (X);
>};
>struct A : B {

"struct B : A", shurely?

>    void foo() throw (Y);
>};
>
>It may be a silly question, but I ask it because the french
>commitee (except me) is convinced that it's ill-formed code.

I think this is covered in Stroustrup 3e, 14.6.1: "A virtual function
may be overriden only by a function that has an
exception-specification at least as restrictive as its own (explicit
or implicit) exception-specification. This rule is really only common
sense. If a derived class threw an exception that the original
function didn't advertise, a caller couldn't be expected to catch it."

- Jim

--
Jim Barry, Thermoteknix Systems Ltd., Cambridge, UK.
http://www.thermoteknix.co.uk - Queen's Award for Export 1998
http://www.geocities.com/SiliconValley/2060




[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/10/10
Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:

> struct X {};
> struct Y : X {};
> struct A { virtual void foo() throw (X); };
> struct A : B { void foo() throw (Y); };

> It may be a silly question, but I ask it because the french
> commitee (except me) is convinced that it's ill-formed code.

According to [except.spec]/7, this is valid:

7 If  a class X is in the type-id-list of the exception-specification of
  a function, that function is said to allow exception objects of  class
  X  or any class publicly and unambiguously derived from X. [snip]

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:oliva@gnu.org mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: William Roeder <roeder@dba-sys.com>
Date: 1998/10/10
Raw View
Valentin Bonnard wrote:

> Does anyone think that the following isn't valid ?

> struct X {};
> struct Y : X {};
>
> struct A {
>     virtual void foo() throw (X);
> };
> struct A : B {
>     void foo() throw (Y);
> };

Invalid since you have 2 struct A's.

struct B : A {
    void foo() throw (Y);
}; // OK


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]