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 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/
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/ 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 type:
|
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"
|
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
|
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 class
first. To make it simple, here is the toy code.
myClass.h
------------------------------------------
#ifndef MYCLASS_H
#define MYCLASS_H
|
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;
|
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: 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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |