make_pair with auto_ptr: ... discards qualifiers

Discussion in 'C++' started by Markus Dehmann, Aug 22, 2006.

  1. I am trying to make a pair with a string and an auto_ptr:

    #include <iostream>
    #include <map>
    using namespace std;

    int main(){
    auto_ptr<int> p(new int(3));
    make_pair("x",p);
    }

    But it won't compile:

    stl_pair.h:85: error: passing `const std::auto_ptr<int>' as `t\
    his' argument of `std::auto_ptr<_Tp>::eek:perator
    std::auto_ptr_ref<_Tp1>() [with _Tp1 = int, _Tp = int]' discards
    qualifiers

    I didn't make p const, so I don't understand why it complains ... How
    can I make it work?
     
    Markus Dehmann, Aug 22, 2006
    #1
    1. Advertising

  2. Markus Dehmann wrote:
    > I am trying to make a pair with a string and an auto_ptr:
    >
    > #include <iostream>
    > #include <map>
    > using namespace std;
    >
    > int main(){
    > auto_ptr<int> p(new int(3));
    > make_pair("x",p);
    > }
    >
    > But it won't compile:
    >
    > stl_pair.h:85: error: passing `const std::auto_ptr<int>' as `t\
    > his' argument of `std::auto_ptr<_Tp>::eek:perator
    > std::auto_ptr_ref<_Tp1>() [with _Tp1 = int, _Tp = int]' discards
    > qualifiers
    >
    > I didn't make p const, so I don't understand why it complains ... How
    > can I make it work?


    You can't make it work. 'std::pair's constructor takes arguments by
    const ref. Inside it constructs the members using copy-construction.
    'std::auto_ptr' constructor takes non-const ref as its argument.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 22, 2006
    #2
    1. Advertising

  3. Markus Dehmann

    Daniel T. Guest

    In article <>,
    "Markus Dehmann" <> wrote:

    > I am trying to make a pair with a string and an auto_ptr:
    >
    > #include <iostream>
    > #include <map>
    > using namespace std;
    >
    > int main(){
    > auto_ptr<int> p(new int(3));
    > make_pair("x",p);
    > }
    >
    > But it won't compile:
    >
    > stl_pair.h:85: error: passing `const std::auto_ptr<int>' as `t\
    > his' argument of `std::auto_ptr<_Tp>::eek:perator
    > std::auto_ptr_ref<_Tp1>() [with _Tp1 = int, _Tp = int]' discards
    > qualifiers
    >
    > I didn't make p const, so I don't understand why it complains ... How
    > can I make it work?


    Items in a pair must have standard copy and assignment semantics, and
    auto_ptr doesn't.
     
    Daniel T., Aug 22, 2006
    #3
    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. danny van elsen
    Replies:
    6
    Views:
    41,252
    Pete Becker
    Apr 24, 2005
  2. Jason
    Replies:
    6
    Views:
    9,083
    Mike Wahler
    Oct 9, 2003
  3. Charlie Zender
    Replies:
    12
    Views:
    1,104
    Peter Pichler
    Jan 3, 2004
  4. Neo
    Replies:
    1
    Views:
    560
    Victor Bazarov
    Mar 28, 2006
  5. Ian Collins

    discards qualifiers

    Ian Collins, May 7, 2006, in forum: C++
    Replies:
    2
    Views:
    771
    John Carson
    May 7, 2006
Loading...

Share This Page