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

  2. 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. Advertisements

  3. Jayden Shui

    Jayden Shui Guest

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

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 (here). After that, you can post your question and our members will help you out.