472,958 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

boost::shared_ptr and operator->()

Something puzzles me about Boost's shared_ptr implementation:

template <typename T>
class shared_ptr
{
public:
T* operator->() const
{
return p;
}
private:
T* p;
};

Why is operator->() const but the return value is non-const? The
Boost documentation answers this very question as follows:

"Shallow copy pointers, including raw pointers, typically don't
propagate constness. It makes little sense for them to do so, as you
can always obtain a non-const pointer from a const one and then
proceed to modify the object through it."

I don't understand this answer. Are they referring to using
const_cast to subvert the constness of shallow copy pointers? If so,
that seems like a weak argument for not enforcing const corectness.

Any insights appreciated.
Jul 22 '05 #1
2 1602
Derek wrote in news:br*************@ID-46268.news.uni-berlin.de:
Something puzzles me about Boost's shared_ptr implementation:

template <typename T>
class shared_ptr
{
public:
T* operator->() const
{
return p;
}
private:
T* p;
};

Why is operator->() const but the return value is non-const? The
Boost documentation answers this very question as follows:

"Shallow copy pointers, including raw pointers, typically don't
propagate constness. It makes little sense for them to do so, as you
can always obtain a non-const pointer from a const one and then
proceed to modify the object through it."

Perhapse this will help (for illustration only), what a shared_ptr would
"look" like when const:

template <typename T>
class shared_ptr_const
{
public:
T* const operator->()
{
return p;
}
private:
T* const p;
};

Note that its T * const p *not* T const *p;

T t;
T *v;
T * const c = &t;
v = c; /* shallow copy ?? (losses toplevel const)*/

The (legal) copy above is the same (in a const-correct sense) as:

int const ic = 2;
int j = ic;

Adding (toplevel) const to a return type (as I did in the
illustration above) is mostly useless.
I don't understand this answer. Are they referring to using
const_cast to subvert the constness of shallow copy pointers? If so,
that seems like a weak argument for not enforcing const corectness.


Nope, that's not what is being discussed.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Rob, your reply makes perfect sense. I overlooked that a const
shared_ptr's member raw pointer is T* const p, not T const *p. Thanks.
Derek wrote in
news:br*************@ID-46268.news.uni-berlin.de:
Something puzzles me about Boost's shared_ptr
implementation:

template <typename T>
class shared_ptr
{
public:
T* operator->() const
{
return p;
}
private:
T* p;
};

Why is operator->() const but the return value
is non-const? The Boost documentation answers
this very question as follows:

"Shallow copy pointers, including raw pointers,
typically don't propagate constness. It makes
little sense for them to do so, as you can always
obtain a non-const pointer from a const one and
then proceed to modify the object through it."


Perhapse this will help (for illustration only), what a
shared_ptr would "look" like when const:

template <typename T>
class shared_ptr_const
{
public:
T* const operator->()
{
return p;
}
private:
T* const p;
};

Note that its T * const p *not* T const *p;

T t;
T *v;
T * const c = &t;
v = c; /* shallow copy ?? (losses toplevel const)*/

The (legal) copy above is the same (in a const-correct
sense) as:

int const ic = 2;
int j = ic;

Adding (toplevel) const to a return type (as I did in the
illustration above) is mostly useless.
I don't understand this answer. Are they referring to
using const_cast to subvert the constness of shallow
copy pointers? If so, that seems like a weak argument
for not enforcing const corectness.


Nope, that's not what is being discussed.

Rob. -- http://www.victim-prime.dsl.pipex.com/

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ctick | last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?
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...
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #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...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
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...
1
by: rssmps | last post by:
Hi, first post and here it goes... This is the current code (working) that I have. It works but for large number of A & B objects, it's not the most efficient. It's also not really heterogenous...
5
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
9
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.