Re: Do exception classes still need no-fail copy constructors, in C++0x?
In article <46A1E5DD.8F11AD8C@this.is.invalid>,
noreply@this.is.invalid (Niels Dekker - no return address) wrote:
When writing a custom exception class in C++03, it's recommended to have
its copy constructor supporting the no-fail guarantee, to prevent the
exception handling mechanism from calling std::terminate(). [E.g., Herb
Sutter & Andrei Alexandrescu - C++ Coding Standards, item 32] Is that
recommendation still relevant in C++0x? I got the impressing that in
C++0x, it's good enough to just make sure that the exception class has a
non-failing move constructor.
Should an STL implementation still take care of proving non-failing copy
constructors for exception classes like std::runtime_error, in C++0x?
Or would it be sufficient to provide non-failing move constructors for
these classes?
The following program in conceptgcc:
#include <iostream>
class A
{
A(const A&);
A& operator=(const A&);
public:
A() {}
A(A&&) {}
};
void f()
{
A a;
throw a;
}
int main()
{
try
{
f();
}
catch (A&)
{
std::cout << "Pass\n";
}
}
Outputs "Pass". Furthermore it doesn't compile if the move constructor
is commented out. That being said, I don't honestly know the answer to
your question. Certainly if you catch by value, instead of by
reference, you'll need a copy ctor.
Also if you use the (brand new, N2179):
std::exception_ptr e = std::current_exception();
You may need a copy constructor as well. And making your exception
types usable with std::current_exception() is probably motivation enough
to keep them copyable (non-mutable refcounted strings are still a
valuable tool :-)).
-Howard
---
[ 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 ]