> Programming Languages > C++
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on C++



C++ - "Code or library or compiler bug?" in Programming Languages


Old 06-20-2004   #1
..f .. ..einba..
 
Default Code or library or compiler bug?

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)?

--
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?
 
Old 06-20-2004   #2
..v.. ..rm..
 
Default Re: Code or library or compiler bug?

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".

 
Old 06-20-2004   #3
..nr.. ..y..
 
Default Re: Code or library or compiler bug?


"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?



 
Old 06-20-2004   #4
..u..
 
Default Re: Code or library or compiler bug?

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 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
 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0