473,809 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_p tr<intpi = new int;
pi = new int;
Thanks

Tim

Jun 1 '07 #1
9 4732
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_p tr<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_p tr<intpi = new int;

can not unwind into

boost::shared_p tr<intpi = boost::shared_p tr<int>(new int);

DS

Jun 1 '07 #2
On 1 Jun, 11:35, dasjotre <dasjo...@googl email.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_p tr<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_p tr<intpi = new int;

can not unwind into

boost::shared_p tr<intpi = boost::shared_p tr<int>(new int);

DS
also, the first

boost::shared_p tr<intpi = new int;

doesn't call the operator= but the constructor.
Jun 1 '07 #3
On 1 Jun, 11:36, dasjotre <dasjo...@googl email.comwrote:
On 1 Jun, 11:35, dasjotre <dasjo...@googl email.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_p tr<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_p tr<intpi = new int;
can not unwind into
boost::shared_p tr<intpi = boost::shared_p tr<int>(new int);
DS

also, the first

boost::shared_p tr<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_p tr<int>(new int);

Jun 1 '07 #4
On Jun 1, 3:35 am, dasjotre <dasjo...@googl email.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_p tr<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_p tr<intpi = new int;

can not unwind into

boost::shared_p tr<intpi = boost::shared_p tr<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...@googl email.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_p tr<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_p tr<intpi = new int;
can not unwind into
boost::shared_p tr<intpi = boost::shared_p tr<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_p tr<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>(n ew int);
DS

Jun 4 '07 #6
On Jun 4, 2:33 am, dasjotre <dasjo...@googl email.comwrote:
On 1 Jun, 16:32, Tim H <thoc...@gmail. comwrote:
On Jun 1, 3:35 am, dasjotre <dasjo...@googl email.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_p tr<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_p tr<intpi = new int;
can not unwind into
boost::shared_p tr<intpi = boost::shared_p tr<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_p tr<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...@googl email.comwrote:
On 1 Jun, 16:32, Tim H <thoc...@gmail. comwrote:
On Jun 1, 3:35 am, dasjotre <dasjo...@googl email.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_p tr<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_p tr<intpi = new int;
can not unwind into
boost::shared_p tr<intpi = boost::shared_p tr<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_p tr<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_p tr<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_p tr<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_p tr<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_p tr<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_p tr<intpi (new int);
also,
boost::shared_p tr<intpi = boost::shared_p tr<int>(new int);
is not equivalent to
boost::shared_p tr<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_p tr<intpi = boost::shared_p tr<int>(new int);
pi = boost::shared_p tr<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_p tr<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_p tr<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_p tr<intpi (new int);
also,
boost::shared_p tr<intpi = boost::shared_p tr<int>(new int);
is not equivalent to
boost::shared_p tr<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_p tr<intpi = boost::shared_p tr<int>(new int);
pi = boost::shared_p tr<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_p tr<intpi = new int;
will try to call boost::shared_p tr<int>::operat or=(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
1692
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
2442
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 double, I have to define a global operator+ (or -*/) and special function classes. --- source begins here ---
6
9061
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 type:
14
2045
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 <boost/shared_ptr.hpp> #include <vector> #include <iterator> #include <iostream> class trial {
2
2138
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 namespace System; #pragma unmanaged
3
4012
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 class first. To make it simple, here is the toy code. myClass.h ------------------------------------------ #ifndef MYCLASS_H #define MYCLASS_H
14
6659
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 it. Specifically, part 1 is obvious to most anyone. Part 2 is isomorphic to part 1, yet behaves differently. If a shared_ptr is const, should it really allow non-const dereferences? Thanks,
6
2551
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 boost::lexical_cast to cast a vector of strings to a string. The compiler I'm using is MSVC++ 2005 Express Edition. The gc++
5
3206
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.