![]() |
Is this a bug of std::unique_ptr?
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 this a bug of the code of unique_ptr in the std template library? Thanks a lot! Jayden |
Re: Is this a bug of std::unique_ptr?
On 16.12.2011 15:28, Jayden Shui wrote:
> > 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 this a bug of the code of unique_ptr in the > std template library? It seems to be a case PEBKAC. <example> [d:\dev\test] > (cl /nologo- 2>&1) | find /i "++" Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86 [d:\dev\test] > cl foo.cpp foo.cpp foo.cpp(7) : error C2440: 'initializing' : cannot convert from 'int *' to 'std::unique_ptr<_Ty>' with [ _Ty=int ] Constructor for class 'std::unique_ptr<_Ty>' is declared 'explicit' with [ _Ty=int ] foo.cpp(8) : error C2440: 'initializing' : cannot convert from 'const std::unique_ptr<_Ty>' to 'std::unique_ptr<_Ty> &' with [ _Ty=int ] Conversion loses qualifiers [d:\dev\test] > (g++ --version 2>&1) | find /i "++" g++ (TDM-2 mingw32) 4.4.1 [d:\dev\test] > g++ -std=c++0x foo.cpp foo.cpp: In function 'int main()': foo.cpp:7: error: conversion from 'int*' to non-scalar type 'const std::unique_ptr<int, std::default_delete<int> >' requested foo.cpp:8: error: invalid initialization of reference of type 'std::unique_ptr<int, std::default_delete<int> >&' from expression of type 'const std::unique_ptr<int, std::default_delete<int> >' [d:\dev\test] > _ </example> Cheers & hth., - Alf |
Re: Is this a bug of std::unique_ptr?
On Dec 16, 9:44*am, "Alf P. Steinbach" <alf.p.steinbach
+use...@gmail.com> wrote: > On 16.12.2011 15:28, Jayden Shui wrote: > > > > > > > > > > > > > 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 this a bug of the code of unique_ptr in the > > std template library? > > It seems to be a case PEBKAC. > > <example> > [d:\dev\test] > *> (cl /nologo- 2>&1) | find /i "++" > Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 > for 80x86 > > [d:\dev\test] > *> cl foo.cpp > foo.cpp > foo.cpp(7) : error C2440: 'initializing' : cannot convert from 'int *' > to 'std::unique_ptr<_Ty>' > * * * * *with > * * * * *[ > * * * * * * *_Ty=int > * * * * *] > * * * * *Constructor for class 'std::unique_ptr<_Ty>' is declared 'explicit' > * * * * *with > * * * * *[ > * * * * * * *_Ty=int > * * * * *] > foo.cpp(8) : error C2440: 'initializing' : cannot convert from 'const > std::unique_ptr<_Ty>' to 'std::unique_ptr<_Ty> &' > * * * * *with > * * * * *[ > * * * * * * *_Ty=int > * * * * *] > * * * * *Conversion loses qualifiers > > [d:\dev\test] > *> (g++ --version 2>&1) | find /i "++" > g++ (TDM-2 mingw32) 4.4.1 > > [d:\dev\test] > *> g++ -std=c++0x foo.cpp > foo.cpp: In function 'int main()': > foo.cpp:7: error: conversion from 'int*' to non-scalar type 'const > std::unique_ptr<int, std::default_delete<int> >' requested > foo.cpp:8: error: invalid initialization of reference of type > 'std::unique_ptr<int, std::default_delete<int> >&' from expression of > type 'const std::unique_ptr<int, std::default_delete<int> >' > > [d:\dev\test] > *> _ > </example> > > Cheers & hth., > > - Alf Sorry, There is a typo in the example code. The corrected version is: #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; } |
Re: Is this a bug of std::unique_ptr?
On 16 joulu, 16:28, Jayden Shui <jayden.s...@gmail.com> wrote:
> I accidently find that the following code works with Visual C++ > compilation So what? Write some real code and stop worrying about non-issues. |
Re: Is this a bug of std::unique_ptr?
In article <3efee69c-4258-4841-a8e5-dd101aacbd4a@t38g2000yqe.googlegroups.com>,
Jayden Shui <jayden.shui@gmail.com> wrote: > >Sorry, There is a typo in the example code. The corrected version is: > >#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; >} g++ certainly doesn't like it: g++ uptr.cpp --std=c++0x uptr.cpp: In function ‘int main()’: uptr.cpp:9: error: invalid initialization of reference of type ‘std::unique_ptr<int, std::default_delete<int> >&’ from expression of type ‘const std::unique_ptr<int, std::default_delete<int> >’ |
| All times are GMT. The time now is 08:38 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.