473,769 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

boost::shared_p tr 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 1690
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_cons t
{
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_cons t
{
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
2920
by: ctick | last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?
6
9058
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:
2
4838
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 "B.h"
2
2137
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
6
4034
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
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
1
1981
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 either. It's really just a storage of the parent class objects. class A { … } class B:public A { … } vector<A*> combined;
5
2917
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
9890
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
3961
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
9422
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
10208
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...
0
9857
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
8867
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...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5294
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
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.