Re: Smart pointers and exceptions
Am 06.09.2011 00:19, schrieb Iker Jamardo Zugaza:
I understand that the runtime cannot solve the
problem but, Ulrich, sorry
SPFooInherited pfi;
SPFoo& pf = pfi;
This of course doesn't compile, because the two are not related.
This code compiles perfectly.
This is probably a compiler defect or some "extension mode" (Do you use
a Visual Studio compiler?). It seems that the compiler allows binding a
temporary to a reference to non-const, which is illegal in standard C++.
While creating the new pf reference, the
smart pointer to Foo is able to check that the conversion from
FooInherited is completely valid:
FooInherited* fi;
Foo* pf = fi;
This is what is done internally in the shared_ptr class (more or less
with some reference counter handling and such ;)).
This is a different story: The shared_ptr implementations typically
provide a constructor template that accepts pointers of derived types.
But the invocation of such a constructor is a conversion and in the
example code discussed this would have the effect to create a temporary
and bind it to an lvalue-reference to non-const, which is non-conforming.
My assumption here is then that the runtime uses a different approach
for calling functions and for catching exceptions even though it could
look like the same at first sight.
Smart pointer constructions are different from real inheritance
relations. The exception machinery depends on real inheritance relations.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]