Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this a bug of std::unique_ptr?

Reply
Thread Tools

Is this a bug of std::unique_ptr?

 
 
Jayden Shui
Guest
Posts: n/a
 
      12-16-2011
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-16-2011
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( : 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
 
Reply With Quote
 
 
 
 
Jayden Shui
Guest
Posts: n/a
 
      12-16-2011
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( : 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;
}
 
Reply With Quote
 
Krice
Guest
Posts: n/a
 
      12-16-2011
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.
 
Reply With Quote
 
none
Guest
Posts: n/a
 
      12-19-2011
In article <3efee69c-4258-4841-a8e5->,
Jayden Shui <> 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> >’



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
ASP.NET Login control bug or SQL 2005 bug? RedEye ASP .Net 2 12-13-2005 10:57 AM
Re: BUG? OR NOT A BUG? John ASP .Net 2 09-21-2005 10:31 AM
Bug Parade Bug 4953793 Michel Joly de Lotbiniere Java 4 12-02-2003 05:05 AM
how to report bug to g++ ? got a bug and fixed up source code DarkSpy C++ 4 06-27-2003 09:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57