make_pair with auto_ptr: ... discards qualifiers

M

Markus Dehmann

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?
 
V

Victor Bazarov

Markus said:
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
 
D

Daniel T.

"Markus Dehmann said:
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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top