Topic: catch by reference and private conversion operator


Author: "Nicola Musatti" <nicola.musatti@gmail.com>
Date: Fri, 3 Mar 2006 11:39:13 CST
Raw View
Hallo,
In experimenting on the design of an exception that cannot be caught I
wrote the following program:

class A {
    operator A& ();
};

int main() {
  try {
    throw A();
  }
  catch ( A & a ) {
  }
}

I expected it to generate an error, but to my surprise two out of my
three compilers accept this code. Who's right?

Thank you,
Nicola Musatti

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Sun, 5 Mar 2006 01:29:15 GMT
Raw View
Nicola Musatti ha scritto:
> Hallo,
> In experimenting on the design of an exception that cannot be caught I
> wrote the following program:
>
> class A {
>     operator A& ();
> };
>
> int main() {
>   try {
>     throw A();
>   }
>   catch ( A & a ) {
>   }
> }
>
> I expected it to generate an error, but to my surprise two out of my
> three compilers accept this code. Who's right?

Why do you expect it to generate an error? I would have said the
opposite, because AFAIK binding an object of type A to an A& is a
"direct binding" and thus it won't involve a call to operator A&().
Moreover 15.3/3 does not consider the presence of conversion operators
when matching handlers to exception objects.

Which compiler gives the error and which error it is?

Ganesh

---
[ 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.comeaucomputing.com/csc/faq.html                      ]