|
|||||
|
|
#1 |
|
|
#include <iostream> // std::cout #include <string> // std::string #include <memory> // std::auto_ptr cl*** Window { public: typedef std::auto_ptr<Window> AutoPtr; virtual ~Window() {} static AutoPtr newObject() { return AutoPtr( new Window ); } }; // cl*** Window cl*** Button: public Window { public: typedef std::auto_ptr<Button> AutoPtr; static AutoPtr newObject() { return AutoPtr( new Button ); } }; // cl*** Button int main() { Window::AutoPtr pWindow( Window::newObject() ); Window::AutoPtr pButton( Button::newObject() ); // <-- std::cout << "Onky donk" << std::endl; } The line marked with an arrow attempts to convert a Button::AutoPtr to a Window::AutoPtr. I had the impression that this should work fine, but compiled with VC 7.1 it just enters an infinite recursion in 'auto_ptr<Button>: peratorauto_ptr_ref<Window>', giving a stack overflow in very short time; is this a bug in the code above, in the standard library implementation, or in the compiler (I know there are subtle issues with temporaries)? -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? |
|
|
#2 |
|
|
P. Steinbach) wrote, >I had the impression that this should work fine, but compiled with VC >7.1 it just enters an infinite recursion in 'auto_ptr<Button>: perator>auto_ptr_ref<Window>', giving a stack overflow in very short time; Beats me. For what it's worth, compiles without error using Digital Mars C++ and STLPORT (free download from http://www.digitalmars.com) and prints "Onky donk". |
|
|
#3 |
|
|
"Alf P. Steinbach" <alfps@start.no> wrote in message news:40d4e4cb.1464853625@news.individual.net... > Code as follows: > > #include <iostream> // std::cout > #include <string> // std::string > #include <memory> // std::auto_ptr > > cl*** Window > { > public: > typedef std::auto_ptr<Window> AutoPtr; > virtual ~Window() {} > static AutoPtr newObject() { return AutoPtr( new Window ); } > }; // cl*** Window > > cl*** Button: public Window > { > public: > typedef std::auto_ptr<Button> AutoPtr; > static AutoPtr newObject() { return AutoPtr( new Button ); } > }; // cl*** Button > > int main() > { > Window::AutoPtr pWindow( Window::newObject() ); > Window::AutoPtr pButton( Button::newObject() ); // <-- > std::cout << "Onky donk" << std::endl; > } > > The line marked with an arrow attempts to convert a Button::AutoPtr to > a Window::AutoPtr. > > I had the impression that this should work fine, but compiled with VC > 7.1 it just enters an infinite recursion in 'auto_ptr<Button>: perator> auto_ptr_ref<Window>', giving a stack overflow in very short time; is > this a bug in the code above, in the standard library implementation, or > in the compiler (I know there are subtle issues with temporaries)? It's a bug in the library. For what it's worth vc7.1 will warn you (unless you turn off warnings:-): warning C4717: 'std::auto_ptr<Button>: perator<Window>std::auto_ptr_ref<Window>' :recursive on all control paths, function will cause runtime stack overflow What's even worse is that vc7.1 will allow to convert to totally unrelated types with exactly the same warning - no error! std:auto_ptr of Metrowerks CodeWarrior 9.2 works fine with this example. Conrad Weyns > > -- > A: Because it messes up the order in which people normally read text. > Q: Why is it such a bad thing? > A: Top-posting. > Q: What is the most annoying thing on usenet and in e-mail? |
|
|
#4 |
|
|
David Harmon <source@netcom.com.invalid> wrote in message news:<40e4ed9b.16403644@news.west.earthlink.net>.. .
> On Sun, 20 Jun 2004 01:20:47 GMT in comp.lang.c++, alfps@start.no (Alf > P. Steinbach) wrote, > >I had the impression that this should work fine, but compiled with VC > >7.1 it just enters an infinite recursion in 'auto_ptr<Button>: perator> >auto_ptr_ref<Window>', giving a stack overflow in very short time; > > Beats me. For what it's worth, compiles without error using Digital > Mars C++ and STLPORT (free download from http://www.digitalmars.com) > and prints "Onky donk". I can't even compile the code..my compiler gives following error: ** cxx: Error: /usr/include/cxx/memory, line 877: member "std::auto_ptr<Button>: wner" is inaccessibledetected during instantiation of "std::auto_ptr<Window>::auto_ptr(const std::auto_ptr<Button> &)" at line 23 of "template_Test.cpp" : owner(a.owner), the_p((_RWSTD_CONST_CAST(auto_ptr<Y>&,a)).release( )) ----------------^ 1 error detected in the compilation of "template_Test.cpp". ** I am compiling it on Digital UNIX. Can it be some Library Issue? regds, AD |