By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,612 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,612 IT Pros & Developers. It's quick & easy.

Code or library or compiler bug?

P: n/a
Code as follows:

#include <iostream> // std::cout
#include <string> // std::string
#include <memory> // std::auto_ptr

class Window
{
public:
typedef std::auto_ptr<Window> AutoPtr;
virtual ~Window() {}
static AutoPtr newObject() { return AutoPtr( new Window ); }
}; // class Window

class Button: public Window
{
public:
typedef std::auto_ptr<Button> AutoPtr;
static AutoPtr newObject() { return AutoPtr( new Button ); }
}; // class 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>::operator
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)?

--
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?
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 20 Jun 2004 01:20:47 GMT in comp.lang.c++, al***@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>::operator
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".

Jul 22 '05 #2

P: n/a

"Alf P. Steinbach" <al***@start.no> wrote in message
news:40*****************@news.individual.net...
Code as follows:

#include <iostream> // std::cout
#include <string> // std::string
#include <memory> // std::auto_ptr

class Window
{
public:
typedef std::auto_ptr<Window> AutoPtr;
virtual ~Window() {}
static AutoPtr newObject() { return AutoPtr( new Window ); }
}; // class Window

class Button: public Window
{
public:
typedef std::auto_ptr<Button> AutoPtr;
static AutoPtr newObject() { return AutoPtr( new Button ); }
}; // class 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>::operator
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>::operator<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?

Jul 22 '05 #3

P: n/a
David Harmon <so****@netcom.com.invalid> wrote in message news:<40***************@news.west.earthlink.net>.. .
On Sun, 20 Jun 2004 01:20:47 GMT in comp.lang.c++, al***@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>::operator
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>::owner" is inaccessible
detected 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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.