Leon
2009-11-18 04:31:24 UTC
Hi,
I know the C++ standard demands that when one throws an exception by value a
temporary copy of the exception object is being made and this temporary
object is passed to the catch site. The copy is made by the static type's
copy constructor.
I am fine with that.
However, the following experiments with VC2003, VC2005 and VC2008 produces
some interesting results and demands that I cannot explain. Perhaps the
community can provide some answer.
Using VC2003:
The compiler demands a public declaration of a copy constructor otherwise
the compiler will tell you that you cannot throw that exception. In the
absence of the implementation, the linker reports unresolved externals. Fair
enough.
Using VC2005 & VC2008
The linker demands an implementation of the copy constructor of the
exception, even if it is a private copy constructor. The compiler does not
complain at all.
Questions:
1) For all these built, the copy constructor is NEVER called. I put a trace
there and I put a break point in a non-trivial copy constructor. I cannot
see the trace nor the debugger breaking in the copy constructor. What is
happening?
2) What is the use of the implementation of a private copy constructor? The
only thing that can use it is a member function of the exception. The throw
statement cannot use it. So why did VC2005/VC2008 was happy with that?
Am I seeing the compiler optimizer at work?
Thanks.
Leon
I know the C++ standard demands that when one throws an exception by value a
temporary copy of the exception object is being made and this temporary
object is passed to the catch site. The copy is made by the static type's
copy constructor.
I am fine with that.
However, the following experiments with VC2003, VC2005 and VC2008 produces
some interesting results and demands that I cannot explain. Perhaps the
community can provide some answer.
Using VC2003:
The compiler demands a public declaration of a copy constructor otherwise
the compiler will tell you that you cannot throw that exception. In the
absence of the implementation, the linker reports unresolved externals. Fair
enough.
Using VC2005 & VC2008
The linker demands an implementation of the copy constructor of the
exception, even if it is a private copy constructor. The compiler does not
complain at all.
Questions:
1) For all these built, the copy constructor is NEVER called. I put a trace
there and I put a break point in a non-trivial copy constructor. I cannot
see the trace nor the debugger breaking in the copy constructor. What is
happening?
2) What is the use of the implementation of a private copy constructor? The
only thing that can use it is a member function of the exception. The throw
statement cannot use it. So why did VC2005/VC2008 was happy with that?
Am I seeing the compiler optimizer at work?
Thanks.
Leon