Regards,
&rzej
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
std::bad_alloc is a special beast which must be throwable
even under conditions where no memory is there ;-)
Note that std::bad_alloc does not have any constructor
that allows provision of a what argument, so presumably
an object of that type just returns a pointer of a char
sequence with static storage duration and a fixed content.
The latter is not true for runtime_error, so this exception
class must be capable to store an arbitrary dynamic
char sequence and must somehow use the free store
to satisfy the post conditions of the constructors.
Of-course these are straw-man arguments. It is possible
to combine both aspects with a sufficient interest. E.g.
instead of using the static storage duration of the string,
a single bad_alloc instance could be created during the
/dynamic initialization/ phase. But even this would not
be sufficient, e.g. the runtime_error class would have
to distinguish between normal copies and shallow copies
or it would need to add reference counting semantics of
a copy.
In any case, it seems that it was decided that the
expected burden on the implementation would be higher
than the advantage of a cleaner exception hierarchy.
To be fair: Since there does not exist very much of
an enforced exception hierarchy in C++ compared
to other languages (e.g. like Java), the expected
overall advantage for the end user is much more
limited.
Greetings from Bremen,
Daniel Kr�gler
> Hi,
> Is there a reason for std::bad_alloc not being a subclass of
> std::runtime_error?
std::bad_alloc has a no throw constructor. The constructor for
std::runtime_error takes a std::string as a parameter, so it may throw
during construction.
--
Nevin ":-)" Liber <mailto:ne...@eviloverlord.com> 773 961-1620