Code or library or compiler bug?

A

Alf P. Steinbach

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>::eek: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)?
 
D

David Harmon

On Sun, 20 Jun 2004 01:20:47 GMT in comp.lang.c++, (e-mail address removed) (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>::eek: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".
 
C

Conrad Weyns

Alf P. Steinbach said:
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>::eek: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>::eek: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

AnujD

David Harmon said:
On Sun, 20 Jun 2004 01:20:47 GMT in comp.lang.c++, (e-mail address removed) (Alf
P. Steinbach) wrote,

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>::eek:wner" 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top