By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

shared_ptr and operator=

P: n/a
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<intpi = new int;
pi = new int;
Thanks

Tim

Jun 1 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 1 Jun, 02:21, Tim H <thoc...@gmail.comwrote:
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<intpi = 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<intconst & p)

can not be used with

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

delete p; // UB !

so

boost::shared_ptr<intpi = new int;

can not unwind into

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

DS

Jun 1 '07 #2

P: n/a
On 1 Jun, 11:35, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 02:21, Tim H <thoc...@gmail.comwrote:
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<intpi = 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<intconst & p)

can not be used with

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

delete p; // UB !

so

boost::shared_ptr<intpi = new int;

can not unwind into

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

DS
also, the first

boost::shared_ptr<intpi = new int;

doesn't call the operator= but the constructor.
Jun 1 '07 #3

P: n/a
On 1 Jun, 11:36, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 11:35, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 02:21, Tim H <thoc...@gmail.comwrote:
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<intpi = 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<intconst & p)
can not be used with
int * p = new int;
void foo(p);
delete p; // UB !
so
boost::shared_ptr<intpi = new int;
can not unwind into
boost::shared_ptr<intpi = boost::shared_ptr<int>(new int);
DS

also, the first

boost::shared_ptr<intpi = 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);

Jun 1 '07 #4

P: n/a
On Jun 1, 3:35 am, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 02:21, Tim H <thoc...@gmail.comwrote:
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<intpi = 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<intconst & 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<intpi = new int;

can not unwind into

boost::shared_ptr<intpi = 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
Jun 1 '07 #5

P: n/a
On 1 Jun, 16:32, Tim H <thoc...@gmail.comwrote:
On Jun 1, 3:35 am, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 02:21, Tim H <thoc...@gmail.comwrote:
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<intpi = 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<intconst & 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<intpi = new int;
can not unwind into
boost::shared_ptr<intpi = 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
(sorry for my clumsy reply)

boost::shared_ptr<intpi = 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

Jun 4 '07 #6

P: n/a
On Jun 4, 2:33 am, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 16:32, Tim H <thoc...@gmail.comwrote:
On Jun 1, 3:35 am, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 02:21, Tim H <thoc...@gmail.comwrote:
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<intpi = 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<intconst & 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<intpi = new int;
can not unwind into
boost::shared_ptr<intpi = 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

(sorry for my clumsy reply)

boost::shared_ptr<intpi = new int;
actually calls the constructor. the consequent
pi = new int;
can not.
g++ is warning about both, which is what confuses me
Jun 4 '07 #7

P: n/a
On 4 Jun, 16:14, Tim H <thoc...@gmail.comwrote:
On Jun 4, 2:33 am, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 16:32, Tim H <thoc...@gmail.comwrote:
On Jun 1, 3:35 am, dasjotre <dasjo...@googlemail.comwrote:
On 1 Jun, 02:21, Tim H <thoc...@gmail.comwrote:
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<intpi = 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<intconst & 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<intpi = new int;
can not unwind into
boost::shared_ptr<intpi = 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
(sorry for my clumsy reply)
boost::shared_ptr<intpi = new int;
actually calls the constructor. the consequent
pi = new int;
can not.

g++ is warning about both, which is what confuses me
(I don't use g++).

in the code:

boost::shared_ptr<intpi = 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<intpi (new int);

line 2 should give you a compile error.

DS
Jun 4 '07 #8

P: n/a
dasjotre wrote:
>g++ is warning about both, which is what confuses me

(I don't use g++).

in the code:

boost::shared_ptr<intpi = 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<intpi (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<intpi (new int);
also,
boost::shared_ptr<intpi = boost::shared_ptr<int>(new int);
is not equivalent to
boost::shared_ptr<intpi (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<intpi = 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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 4 '07 #9

P: n/a
On 4 Jun, 16:45, "Thomas J. Gritzan" <Phygon_ANTIS...@gmx.dewrote:
dasjotre wrote:
g++ is warning about both, which is what confuses me
(I don't use g++).
in the code:
boost::shared_ptr<intpi = 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<intpi (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<intpi (new int);
also,
boost::shared_ptr<intpi = boost::shared_ptr<int>(new int);
is not equivalent to
boost::shared_ptr<intpi (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<intpi = 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<intpi = new int;
will try to call boost::shared_ptr<int>::operator=(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

Jun 4 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.