473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

shared_ptr and operator=

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
9 4707
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Derek | last post by:
Something puzzles me about Boost's shared_ptr implementation: template <typename T> class shared_ptr { public: T* operator->() const { return p; }
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
3
by: zl2k | last post by:
hi, all Here is what I want to do: to wrap my self defined class in a shared_ptr and then insert it into the priority_queue. The priority_queue should pump the least element of the self defined...
14
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows...
6
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.