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

J

Jayden Shui

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
 
A

Alf P. Steinbach

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

Jayden Shui

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.
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top