473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Smart Pointer Question

I have been thinking about this and it may have already been thrashed
out and hung out to dry as a topic of no more interest but here goes.

I found when implementing a smart pointer that the typical
implementation goes like:

template<typename T>
class SmartPointer
{
// other stuff

T* operator->() const;
};

Now, when we pass around such a pointer in the following form:

void foo(const SmartPointer<X>& ptr)
{
ptr->Function1();
}

Function1() can be a function that can change the state of the
underlying object. Being a reference ptr of course cannot be
reassigned. Consequently, since we are looking at a "pointer" albeit a
smart one, we may want to actually implement the smart pointer as
follows:

template<class T,
class TMutableInterface = T
class TImmutableInterface = T>
class SmartPointer
{
// other stuff

TMutableInterface* operator->();
TImmutableInterface* operator->() const;
}

This has the following effect:

For "const SmartPointer<X, XMI, XImI>& ptr"

ptr->Function1() will only be allowed if Function1() is specified by
the "ImmutableInterface of X" that is "XImI".

and for "SmartPointer<X, XMI, XImI>& ptr"

ptr->Function2() will only be allowed if Function2() is specified by
the "MutableInterface of X" that is "XMI".

Thus in effect:

const SmartPointer<T, TMI, TImI>& ptr <=> T const* const ptr

and

SmartPointer<T, TMI, TImI>& ptr <=> T const* ptr

-------

I wonder if this has ever been explored? If so, can someone tell me how
they used it? I have been writing some utilities as part of my work,
where I find it useful to return a "const SmartPointer&" and
"SmartPointer&" depending on what access I want the caller to have.

Comments?

-vijai.

Sep 7 '05 #1
3 1894
Vijai Kalyan wrote:
I have been thinking about this and it may have already been thrashed
out and hung out to dry as a topic of no more interest but here goes.

I found when implementing a smart pointer that the typical
implementation goes like:

NOTE I CHANGED THE NAMES TO SmartPointerA and SmartPointerB

template<typename T>
class SmartPointerA
.... template<class T,
class TMutableInterface = T
class TImmutableInterface = T>
class SmartPointerB
{
// other stuff

TMutableInterface* operator->();
TImmutableInterface* operator->() const;
}

Are these two equivalent ?

SmartPointerA<TIM> x ReturnConst()

const SmartPointerB<T,TM,TI> x ReturnConst()

Would that mean that SmartPointerB is far more verbose for no good reason ?

Does this also mean that you tie the constness of the smart pointer to
the accessibility of the object being pointed to which really should be
2 separate things ?

G
Sep 7 '05 #2
Vijai Kalyan wrote:
I have been thinking about this and it may have already been thrashed
out and hung out to dry as a topic of no more interest but here goes.

I found when implementing a smart pointer that the typical
implementation goes like:

template<typename T>
class SmartPointer
{
// other stuff

T* operator->() const;
};

Now, when we pass around such a pointer in the following form:

void foo(const SmartPointer<X>& ptr)
{
ptr->Function1();
}

Function1() can be a function that can change the state of the
underlying object. Being a reference ptr of course cannot be
reassigned. Consequently, since we are looking at a "pointer" albeit a
smart one, we may want to actually implement the smart pointer as
follows:

template<class T,
class TMutableInterface = T
class TImmutableInterface = T>
class SmartPointer
{
// other stuff

TMutableInterface* operator->();
TImmutableInterface* operator->() const;
}

This has the following effect:

For "const SmartPointer<X, XMI, XImI>& ptr"

ptr->Function1() will only be allowed if Function1() is specified by
the "ImmutableInterface of X" that is "XImI".

and for "SmartPointer<X, XMI, XImI>& ptr"

ptr->Function2() will only be allowed if Function2() is specified by
the "MutableInterface of X" that is "XMI".

Thus in effect:

const SmartPointer<T, TMI, TImI>& ptr <=> T const* const ptr

and

SmartPointer<T, TMI, TImI>& ptr <=> T const* ptr

-------

I wonder if this has ever been explored? If so, can someone tell me how
they used it? I have been writing some utilities as part of my work,
where I find it useful to return a "const SmartPointer&" and
"SmartPointer&" depending on what access I want the caller to have.

Comments?


The trick here is that you are transferring the constness of the
pointer to the pointee, which is a feature some have objected to
because it doesn't match the behavior of raw pointers (TR1/Boost's
smart pointers, for instance, are as close to raw pointers as possible
and don't support transferring the constness). However, that behavior
does match std::vector, which, as a "smart array" of sorts, diverges
from the behavior of raw arrays at the point of constness.

You'll need to watch out for copying to get rid of const, but your
policy-based pointer may take care of that automatically depending on
its copy constructors and assignment operators. In particular,
consider:

struct A { void Mutate(); }

void Foo( const SmartPointer<A,A,const A>& ptr )
{
ptr->Mutate(); // Error!

SmartPointer<A>& backDoor( ptr ); // Error?
backDoor->Mutate(); // No error here
}

Does SmartPointer<A> accept SmartPointer<A,A,const A> for copying?

If you disable copy constructors and assignment operators altogether by
making them private, undefined functions, then SmartPointer can't be
used in standard containers, which is a major drawback (one that
TR1/Boost's smart pointers avoids).

I have used this same technique for smart pointers that are members of
a class, so that if the class is const, the pointees become const also.
This behavior is not *always* desirable, but I find that it is
*usually* what I want and, as with std::vector, the compiler can more
closely watch my const-correctness and catch errors.

M

Sep 7 '05 #3
Yes, I did want to transfer the constness of the smart pointer to the
pointee. The reason is because I felt that since

SmartPointer <=> T const*

then maybe

const SmartPointer <=> T const* const

which seems consistent to me, although as you have pointed out copying
can workaround rid of this.

-vijai.

Sep 7 '05 #4

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

Similar topics

4
by: Eric | last post by:
See question in main function below...TIA. struct A {}; struct B: public A {}; #include <boost/shared_ptr.hpp> #include <set> typedef boost::shared_ptr<A> AP; typedef std::set<AP> AS;
2
by: Vijayaraghavan Kalyanapasupathy | last post by:
Hello All, I just learnt about smart pointers. Given the advantages of using smart-pointers, as a designer of say a List object, I can inhibit creation of the actual objects by making its...
11
by: lokb | last post by:
Hi, I have a structure which and defined a smart pointer to the structure. /* Structure of Begin Document Index Record */ typedef struct BDI_Struct{ unsigned char rname; unsigned short int...
6
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles,...
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
14
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.