Re: C++0x/1x exception specifications proposal: Compile-time checked
Ioannis Vranos ha scritto:
Perhaps a mechanism can be introduced in the C++0x/1x standard,
I still prefer calling it C++0x and wish it can be approved before the
end of the decade...
something simple like defining a function as:
void somefunc(void) throw()
{
// ...
}
and getting a compile time error saying something like:
"Error: void somefunc(void) throw(): Wrong exception specification.
somefunc can throw std::bad_alloc, std::range_error".
That is make the compiler to check exception specifications for errors too.
Think about it. A function without exception specification can
potentially throw any exception. Most C++ library functions don't have
exception specifications. In addition to those functions borrowed from
the C library, there are a lot of cases of C++ functions with EH
guarantees that cannot be represented with exception specifications, for
example most STL algorithms. For instance:
int foo(const std::vector<int> v) throw()
{
return std::accumulate(v.begin(), v.end(), 0);
}
this wouldn't compile according to your proposal, because
std::accumulate does not have an exception specification. However this
specific instantiation of std::accumulate is guaranteed not to throw.
There's no way you can describe with exception specifications that
std::accumulate does not throw exceptions itself, but it might propagate
an exception thrown during the manipulation of an UDT passed as template
parameter.
Your proposal would actually make it impossible to call most library
functions from any function with an exception specification. And not
only direct calls, even calls at an arbitrary level of nesting. In
practice, a function with an exception specification could barely
perform any non-trivial task.
Of course, I'm omitting the fact that your proposal would make illegal
practically every C++ program written so far with at least one exception
specification in it.
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]