Re: C++ Exception and Copy Constructor requirement
"Leon" wrote:
Never catch by value as it will cause slicing and extra copying.
I hope that was a typo.
No it wasn't a typo. I intentionally wrote it that way to
emphasize that copy constructor is required.
Now if you comment out the implementation of the private copy
cater of CExcept and include just a declaration, the standard
way of blocking copying, just like that for the operator=(), the
compilation is still successful. But the linker fails as it
cannot find the implementation.
Sounds like a bug to me. The following code should fail, but
compiles cleanly with VC++2008:
<code>
class X
{
public:
X() {}
private:
X(const X&) {}
};
int main()
{
try
{
throw X();
}
catch(X&)
{
}
return 0;
}
</code>
With the code above, you should not see the trace "Copy C'tor"
after a run. If you put a break point there, it does not stop.
If that is the case, why demands the implementation?
As far as exception object is concerned, throwing it by value is
no different from passing an object by value as a function
argument or returning it by value from a function with `return'
statement. In all these cases the compiler is allowed to eliminate
redundant copy. In any case, the compiler is required by the
Standard to check the availability of copy constructor as if
actual copying were performed. (See
http://en.wikipedia.org/wiki/Return_value_optimization for more
info.)
That's the puzzling bit and I hope someone can shed some light
on this.
It looks like a bug in the compiler. An exception object with
inaccessible (private or protected) copy constructor should not
pass compilation.
Alex