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. 2 1639
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/
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ctick |
last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from
standard library?
|
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...
|
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...
|
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...
|
by: Toby Bradshaw |
last post by:
Hi,
Consider the following:
class A
{
public:
virtual bool foo() = 0;
};
|
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...
|
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...
|
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);
|
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?
|
by: EnsGabe |
last post by:
Suppose you have a class heirarchy as such:
class Base{
....
};
class Mid1 : public Base{
....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |