Code or library or compiler bug?

Discussion in 'C++' started by Alf P. Steinbach, Jun 20, 2004.

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

    --
    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?
    Alf P. Steinbach, Jun 20, 2004
    #1
    1. Advertising

  2. Alf P. Steinbach

    David Harmon Guest

    On Sun, 20 Jun 2004 01:20:47 GMT in comp.lang.c++, (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".
    David Harmon, Jun 20, 2004
    #2
    1. Advertising

  3. Alf P. Steinbach

    Conrad Weyns Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    > 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: 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?
    Conrad Weyns, Jun 20, 2004
    #3
  4. Alf P. Steinbach

    AnujD Guest

    David Harmon <> wrote in message news:<>...
    > On Sun, 20 Jun 2004 01:20:47 GMT in comp.lang.c++, (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".



    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
    AnujD, Jun 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan
    Replies:
    0
    Views:
    1,107
  2. Ram
    Replies:
    0
    Views:
    2,805
  3. Michael T Decerbo
    Replies:
    3
    Views:
    409
    Michael T Decerbo
    Oct 21, 2003
  4. Replies:
    4
    Views:
    291
    Thomas Tutone
    Jul 27, 2006
  5. SzH
    Replies:
    1
    Views:
    317
    mlimber
    Apr 20, 2007
Loading...

Share This Page