Re: std::auto_ptr exception
Thank, Igor.
Using void * allows the following code
#include "stdafx.h"
template <typename T>
class A
{
public:
A( void * initValue ): vp( initValue ) {}
A( const A & rhs ): vp( rhs.vp ) {}
~A() { delete ( T *)vp; }
private:
void *vp;
};
class B {};
int _tmain(int argc, _TCHAR* argv[])
{
B *bp = new B;
{
A<int> a = bp;
}
return 0;
}
Vladimir Grigoriev
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:%23xMk%23qMnJHA.3876@TK2MSFTNGP02.phx.gbl...
"Vladimir Grigoriev" <vlad.moscow@mail.ru> wrote in message
news:uLNWYQMnJHA.5228@TK2MSFTNGP02.phx.gbl
I tried the following example by using Visual C++ 2005 EE
#include "stdafx.h"
#include <memory>
std::auto_ptr<int> source()
{
return new int( 10 );
}
This shouldn't compile - there is no implicit conversion from int* to
std::auto_ptr<int>. You have to write
return std::auto_ptr<int>(new int(10));
The run-time exception (due to double destruction) is just a consequence
of allowing an incorrect program to compile in the first place. See
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101842
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925