Topic: std exceptions - intended interface?
Author: Alan Bowsher <ajb@iti-oh.com>
Date: 1996/12/31 Raw View
Hello,
I am trying to temporarily implement the standard exception classes
until all the compilers we use implement them. I have a question about
the intended interface for the constructors of these classes.
It appears (according the Jan 96 spec) that there is just one
constructor that can be used to specify the string value that these
classes contain - it has a single argument of const string&. During some
testing on a Sun box, I found that I could specify a const char * and
the compiler would do the type conversion for me, so I could say:
throw logic_error ("Oops");
instead of having to say something like:
throw logic_error (string s1("Oops"));
However, I noticed that MSVC++ has implemented the standard exception
constructors with the keyword explicit. I'm a little vague on
'explicit' - won't that prevent me from using the first method above?
If so, is this what the spec intended? The first method seems so much
more natural.
Thanks for any input,
Alan
------------------------------------------------------------------------
------
Alan Bowsher International TechneGroup Inc.
ajb@iti-oh.com http://www.iti-oh.com
(513) 576-3832 (Voice) (513) 576-3994 (FAX)
---
[ 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
]
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: jlilley@empathy.com (John Lilley)
Date: 1996/12/31 Raw View
Alan Bowsher wrote:
> I am trying to temporarily implement the standard exception classes [...]
>
> During some
> testing on a Sun box, I found that I could specify a const char * and
> the compiler would do the type conversion for me, so I could say:
>
> throw logic_error ("Oops");
>
> However, I noticed that MSVC++ has implemented the standard exception
> constructors with the keyword explicit. I'm a little vague on
> 'explicit' - won't that prevent me from using the first method above?
> If so, is this what the spec intended? The first method seems so much
> more natural.
'explicit' before a constructor means that the constructor will not be
considered for *automatic* conversions from the type of the constructor
argument to the type of the class.
Thus, your code should still work for two reasons:
1) The explicit keyword is attached to the logic_error constructor.
This means that there is no *automatic* conversion of string to logic
error. So the following won't work:
void foo(logic_error le);
foo(string("hi")); // error -- no implicit conversion of
// string to logic_error
However, there is still automatic conversion from const char* to string:
void foo(logic_error le);
foo(logic_error("hi")); // OK -- explicit logic_error construction
// OK -- implicit string conversion
2) MSVC++ 4.2 does not implement 'explicit'. Their standard library
headers define it away, like they do with 'bool', in the file
<xstddef>.
john lilley
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]