Please read this: Is there a bug in std::unique_ptr? (Repost, thecode in old post is wrong)

Discussion in 'C++' started by Jayden Shui, Dec 16, 2011.

  1. Jayden Shui

    Jayden Shui Guest

    Hi All,

    I accidently find that the following code works with Visual C++
    compilation

    #include <memory>
    #include <iostream>
    using namespace std;

    int main()
    {
    unique_ptr<int> const p(new int(1));
    unique_ptr<int>& q = p; // assign a constant to a non-constant
    reference
    q.reset(new int(2));
    cout << *p; // output 2
    return 0;
    }

    My question is from the 2nd statement of assigning a constant to a non-
    constant reference. I think the compiler should report an compilation
    error, but it doesn't. Is there a bug in the code of unique_ptr in the
    std template library?

    Thanks a lot!

    Jayden
    Jayden Shui, Dec 16, 2011
    #1
    1. Advertising

  2. Re: Please read this: Is there a bug in std::unique_ptr? (Repost,the code in old post is wrong)

    On 16.12.2011 15:54, Jayden Shui wrote:
    > Hi All,
    >
    > I accidently find that the following code works with Visual C++
    > compilation
    >
    > #include<memory>
    > #include<iostream>
    > using namespace std;
    >
    > int main()
    > {
    > unique_ptr<int> const p(new int(1));
    > unique_ptr<int>& q = p; // assign a constant to a non-constant
    > reference
    > q.reset(new int(2));
    > cout<< *p; // output 2
    > return 0;
    > }
    >
    > My question is from the 2nd statement of assigning a constant to a non-
    > constant reference. I think the compiler should report an compilation
    > error, but it doesn't. Is there a bug in the code of unique_ptr in the
    > std template library?


    What did you fail to understand about my answer?

    It does not compile with Visual C++ 10.0, and it does not compile with
    MinGW g++ 4.4.1.

    Show your compilation that works, stop spamming/trolling.
    Alf P. Steinbach, Dec 16, 2011
    #2
    1. Advertising

  3. Jayden Shui

    Jayden Shui Guest

    On Dec 16, 10:45 am, "Alf P. Steinbach" <alf.p.steinbach
    > wrote:
    > On 16.12.2011 15:54, Jayden Shui wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > Hi All,

    >
    > > I accidently find that the following code works with Visual C++
    > > compilation

    >
    > > #include<memory>
    > > #include<iostream>
    > > using namespace std;

    >
    > > int main()
    > > {
    > >     unique_ptr<int>  const p(new int(1));
    > >     unique_ptr<int>&  q = p; // assign a constant to a non-constant
    > > reference
    > >     q.reset(new int(2));
    > >     cout<<  *p;  // output 2
    > >     return 0;
    > > }

    >
    > > My question is from the 2nd statement of assigning a constant to a non-
    > > constant reference. I think the compiler should report an compilation
    > > error, but it doesn't. Is there a bug in the code of unique_ptr in the
    > > std template library?

    >
    > What did you fail to understand about my answer?
    >
    > It does not compile with Visual C++ 10.0, and it does not compile with
    > MinGW g++ 4.4.1.
    >
    > Show your compilation that works, stop spamming/trolling.


    Sorry for disturbing. I found the problem. I am in fact using intel c+
    + in visual studio. Intel c++ compiler passes it by treating it as
    only a warning.
    Jayden Shui, Dec 16, 2011
    #3
    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. Peter Jansson
    Replies:
    5
    Views:
    6,294
    Ivan Vecerina
    Mar 17, 2005
  2. Micha³ 'Khorne' Rzechonek

    C++0x: unique_ptr and std::move

    Micha³ 'Khorne' Rzechonek, Jan 28, 2009, in forum: C++
    Replies:
    7
    Views:
    6,919
  3. Jayden Shui

    Is this a bug of std::unique_ptr?

    Jayden Shui, Dec 16, 2011, in forum: C++
    Replies:
    4
    Views:
    816
  4. Brice Gagnage
    Replies:
    6
    Views:
    1,538
    Brice Gagnage
    Apr 5, 2012
  5. Martin Ba
    Replies:
    1
    Views:
    289
    Martin Ba
    Nov 23, 2012
Loading...

Share This Page