Is this a bug of std::unique_ptr?

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 this a bug of the code of unique_ptr in the
    std template library?

    Thanks a lot!

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

  2. 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
     
    Alf P. Steinbach, Dec 16, 2011
    #2
    1. Advertising

  3. Jayden Shui

    Jayden Shui Guest

    On Dec 16, 9:44 am, "Alf P. Steinbach" <alf.p.steinbach
    > 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;
    }
     
    Jayden Shui, Dec 16, 2011
    #3
  4. Jayden Shui

    Krice Guest

    On 16 joulu, 16:28, Jayden Shui <> 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.
     
    Krice, Dec 16, 2011
    #4
  5. Jayden Shui

    none Guest

    In article <>,
    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> >’
     
    none, Dec 19, 2011
    #5
    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. Aaron Graham

    where is unique_ptr?

    Aaron Graham, Jun 20, 2008, in forum: C++
    Replies:
    1
    Views:
    661
    Ed Smith-Rowland
    Jun 22, 2008
  2. Micha³ 'Khorne' Rzechonek

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

    Micha³ 'Khorne' Rzechonek, Jan 28, 2009, in forum: C++
    Replies:
    7
    Views:
    6,970
  3. Jayden Shui
    Replies:
    2
    Views:
    371
    Jayden Shui
    Dec 16, 2011
  4. Brice Gagnage
    Replies:
    6
    Views:
    1,623
    Brice Gagnage
    Apr 5, 2012
  5. Martin Ba
    Replies:
    1
    Views:
    304
    Martin Ba
    Nov 23, 2012
Loading...

Share This Page