shared_ptr and operator=

T

Tim H

Why is the following code not valid? I mean, I see the code and it
doesn't allow it, but I am curious about the rationale?

boost::shared_ptr<int> pi = new int;
pi = new int;


Thanks

Tim
 
D

dasjotre

Why is the following code not valid? I mean, I see the code and it
doesn't allow it, but I am curious about the rationale?

boost::shared_ptr<int> pi = new int;
pi = new int;

(you are aware that there is no operator=(raw ptr) in shared_ptr)

IMHO the clue is in the explicit constructor of shared_ptr

so something like

void foo(shared_ptr<int> const & p)

can not be used with

int * p = new int;
void foo(p);

delete p; // UB !

so

boost::shared_ptr<int> pi = new int;

can not unwind into

boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);

DS
 
D

dasjotre

(you are aware that there is no operator=(raw ptr) in shared_ptr)

IMHO the clue is in the explicit constructor of shared_ptr

so something like

void foo(shared_ptr<int> const & p)

can not be used with

int * p = new int;
void foo(p);

delete p; // UB !

so

boost::shared_ptr<int> pi = new int;

can not unwind into

boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);

DS

also, the first

boost::shared_ptr<int> pi = new int;

doesn't call the operator= but the constructor.
 
D

dasjotre

also, the first

boost::shared_ptr<int> pi = new int;

doesn't call the operator= but the constructor.

(sorry I haven't had my coffee yet)

I meant

pi = new int;

can not unwind into

pi = boost::shared_ptr<int>(new int);
 
T

Tim H

(you are aware that there is no operator=(raw ptr) in shared_ptr)

IMHO the clue is in the explicit constructor of shared_ptr

so something like

void foo(shared_ptr<int> const & p)

can not be used with

int * p = new int;
void foo(p);

delete p; // UB !

Ahh, of course. I get that, then.
boost::shared_ptr<int> pi = new int;

can not unwind into

boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);

This is supposed to call the constructor, isn't it? Why can it not
figure out (or not be allowed) to call the (int *) ctor?

Thanks

Tim
 
D

dasjotre

Ahh, of course. I get that, then.




This is supposed to call the constructor, isn't it? Why can it not
figure out (or not be allowed) to call the (int *) ctor?

Thanks

Tim

(sorry for my clumsy reply)

boost::shared_ptr<int> pi = new int;
actually calls the constructor. the consequent
pi = new int;
can not. since pi is already constructed object
the assignment operator is looked up and there
are two assignment operators in shared_ptr
one that takes shared_ptr and one that takes
std::auto_ptr. neither of these two are implicitly
constructible from raw pointer, so neither of them
can be used.

that is, compiler can not convert
pi = new int;
into either
pi = shared_ptr<int>(new int);
or
pi = auto_ptr<int>(new int);


DS
 
T

Tim H

(sorry for my clumsy reply)

boost::shared_ptr<int> pi = new int;
actually calls the constructor. the consequent
pi = new int;
can not.

g++ is warning about both, which is what confuses me
 
D

dasjotre

g++ is warning about both, which is what confuses me

(I don't use g++).

in the code:

boost::shared_ptr<int> pi = new int; // 1
pi = new int; // 2

line 1 should not give you any warning at
all since it is equivalent to
boost::shared_ptr<int> pi (new int);

line 2 should give you a compile error.

DS
 
T

Thomas J. Gritzan

dasjotre said:
(I don't use g++).

in the code:

boost::shared_ptr<int> pi = new int; // 1
pi = new int; // 2

line 1 should not give you any warning at
all since it is equivalent to
boost::shared_ptr<int> pi (new int);

line 2 should give you a compile error.

shared.cpp:1: error: conversion from 'int*' to non-scalar type
'boost::shared_ptr<int>' requested
shared.cpp:2: error: no match for 'operator=' in 'pi = (int*)operator new(4u)'

line 1 is not equivalent to boost::shared_ptr<int> pi (new int);
also,
boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);
is not equivalent to
boost::shared_ptr<int> pi (new int);
since you need a copy constructor for the first (the compiler complains if
you haven't, but it may optimize it away).

So in both lines you need to _explicitly_ construct a temporary shared_ptr:

boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);
pi = boost::shared_ptr<int>(new int);

This was made so that the compiler does not accidentaly construct a
shared_ptr from a raw pointer and free it afterwards. This could cause bugs
like double frees or calling delete on automatic (stack)-objects.
 
D

dasjotre

shared.cpp:1: error: conversion from 'int*' to non-scalar type
'boost::shared_ptr<int>' requested
shared.cpp:2: error: no match for 'operator=' in 'pi = (int*)operator new(4u)'

line 1 is not equivalent to boost::shared_ptr<int> pi (new int);
also,
boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);
is not equivalent to
boost::shared_ptr<int> pi (new int);
since you need a copy constructor for the first (the compiler complains if
you haven't, but it may optimize it away).

So in both lines you need to _explicitly_ construct a temporary shared_ptr:

boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);
pi = boost::shared_ptr<int>(new int);

This was made so that the compiler does not accidentaly construct a
shared_ptr from a raw pointer and free it afterwards. This could cause bugs
like double frees or calling delete on automatic (stack)-objects.


I stand corrected.

1. will call
boost::shared_ptr<int> pi = new int;
will try to call boost::shared_ptr<int>::eek:perator=(raw ptr)
and will fail for the same reason as 2.
pi = new int;

most compilers will call constructor on 1 (not standard
behaviour), and I got lost thinking about the rationale
for explicit shared_ptr/auto_ptr constructors.

DS
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top