Topic: Refence primitives in C++
Author: Steve Clamage <stephen.clamage@Eng.Sun.COM>
Date: 1997/09/02 Raw View
sam varghese wrote:
>
> Steve Clamage wrote:
> >
> > sam varghese wrote:
> > >
> > > My undersanding on reading Stroustroup's book is that one cannot
> > > implement a true primitive reference type in C++. Is this true?
> >
> > What do you mean by "implementing a true primitive reference
> > type in C++"?
>
> What I mean by true primitive type is that it has all the properties
> of a regular type and yet behaves like a built-in type like pointers and
> integers. Reference is a concept built into the C++ language; yet does
> it have the same footing as the humble built-in pointer?
>
> If not; can one define a true regular type like the way we define the
> STL types like list,map etc? ... is it possible to define a true reference
> type that a casual user can use and understand without being bogged down
> in subtleties.
Well, pointers, integers and floating-point types (for example) all
behave
differently, so I don't think your question makes a lot of sense. Each
category of type has different sorts of restrictions. You can add two
integer or floating types, but not two pointers. You can perform bitwise
operations on integers but not on floating or pointer types. You
can dereference a pointer but not an integer or floating-point object.
If you complain that the differences between pointers and references
are too subtle, I think you must address the same complaint to all
categories of types.
I suspect what you are really asking is why references don't have all
the properties of pointers, and can you simulate a reference type that
does.
As you can read in D&E, it was a deliberate language design decision
that references do not have all the properties of pointers. A pointer
can be set to point to no object or to different objects. A reference
always refers to one actual object (assuming you don't invoke undefined
behavior). Since you don't have a way to get at the reference itself,
you can't have pointers or references to references nor arrays of
references. (The semantics just don't work.)
You choose to use a pointer or reference based on which set
of properties you want; each set has its own benefits. (Not unlike
choosing among integer and floating-point categories.)
To answer the second part of the question, I don't think it is possible
to simulate a reference type that has pointer-like properties. For one
thing, you would want to overload operator dot, which is not allowed.
(And that is yet another controversial restriction.)
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/09/03 Raw View
sam varghese <samvarghese@sprintmail.com> asked:
> My undersanding on reading Stroustroup's book is that one cannot
> implement a true primitive reference type in C++. Is this true?
>
> If true, why ???
True. Because once a reference is initialized (to refer to some object)
it can never be changed, even if the object it refers to is destructed.
'True' references, such as those found in Java, Eiffel, and Smalltalk, can
be modified freely (to point to other objects) and can also be set to
'void' or 'null' (meaning that they don't point to any object). C++ can't
do this; it has no concept of a 'void' or modifiable reference.
--------------------. BEA Systems, Inc. ,-. +1-972-738-6125 Office
David R. Tribble \ ,------------------' \ +1-972-738-6111 Fax
http://www.beasys.com `-' Dallas, TX 75248 `-----------------------
david.tribble@noSPAM.beasys.com http://www.flash.net/~dtribble
-------------------------------------------------------------------------
Send junk to: postmaster@agis.net mrchig@easynet.co.uk
emailer@qlink2info.com simrem@answerme.com office@canma.dyn.ml.org
markdan3@earthlink.net clover@earthfriends.com office@savetrees.com
abuse@cyberpromo.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1997/09/04 Raw View
In article <340EC351.167E@wizard.net>, James Kuyper <kuyper@wizard.net> wrote:
>David R Tribble wrote:
>> 'True' references, such as those found in Java, Eiffel, and Smalltalk, can
>> be modified freely (to point to other objects) and can also be set to
>> 'void' or 'null' (meaning that they don't point to any object). C++ can't
>> do this; it has no concept of a 'void' or modifiable reference.
>
>I'm not familiar with those languages. How does a 'true' reference
>differ from a C++ pointer (ignoring purely sytactical details such as
>'&', '*', and '->')? What can you do with a 'true' reference that cannot
>be done with a C++ pointer?
I'm not sure I like the term, but what these other languages (and also
Lisp and Dylan) have in common is that the pointers are all hidden and
automatically dereferenced as necessary. For instance, in the Lisp code:
(setf a (list 1 2 3))
(setf b a)
a and b now reference the same list. If you then do
(setf (car a) 4)
(print (car b))
4 will be printed. In C or C++, you would have to use explicit pointers
to make this happen. In C++ you could make b a reference to a, but then
you run into the fact that you can't change what a reference refers to,
i.e. you can't do the equivalent of
(setf c (list 7 8 9))
(setf b c)
In particular, I don't think there's a way with C++ references to do
something like:
(if (> (car a) (car c))
(setf b a)
(setf b c))
(however, I suspect I may be wrong about this, and I'm sure someone will
leap to show some convoluted way of doing it involving calling a function
that returns a reference).
--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Christian Millour <chris161@club-internet.fr>
Date: 1997/09/08 Raw View
Barry Margolin wrote:
> In particular, I don't think there's a way with C++ references to do
> something like:
>
> (if (> (car a) (car c))
> (setf b a)
> (setf b c))
>
Whatever & b = *(somecondition
? static_cast<Whatever *>(&a)
: static_cast<Whatever *>(&c));
The casts are here just in case a and c are instances of classes
derived from Whatever. I wonder whether they are necessary, even
in that case.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Marcelo Cantos <marcelo@janus.mds.rmit.edu.au>
Date: 1997/09/08 Raw View
"Bradd W. Szonye" <bradds@concentric.net> writes:
> Finally, point 3 is where OL and C++ references diverge the most. If
> there's any problem with reseatable references, it's that (a) you're not as
> confident about what you're referring to, and (b) the syntactic sugar of
> references [point 5] can cause some confusion over whether you're assigning
> to the reference or what it refers to. C++ at least avoids that issue,
> which is why I think Stroustrup made them non-reseatable.
I would have thought that `&a = b' was pretty unambiguous. However, I
agree that the issue is trivial. If you want to reassign, use
pointers and be done with it!
Cheers,
Marcelo
--
______________________________________________________________________
Marcelo Cantos, Research Assistant __/_ marcelo@mds.rmit.edu.au
Multimedia Database Systems Group, RMIT / _ Tel 61-3-9282-2497
L2/723 Swanston St, Carlton VIC 3053, Aus/ralia ><_>Fax 61-3-9282-2490
Acknowledgements: errors - me; wisdom - God; sponsorship - RMIT
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Marcelo Cantos <marcelo@janus.mds.rmit.edu.au>
Date: 1997/09/08 Raw View
Barry Margolin <barmar@bbnplanet.com> writes:
>
> In article <340EC351.167E@wizard.net>, James Kuyper <kuyper@wizard.net> wrote:
> >David R Tribble wrote:
> >> 'True' references, such as those found in Java, Eiffel, and Smalltalk, can
> >> be modified freely (to point to other objects) and can also be set to
> >> 'void' or 'null' (meaning that they don't point to any object). C++ can't
> >> do this; it has no concept of a 'void' or modifiable reference.
> >
> >I'm not familiar with those languages. How does a 'true' reference
> >differ from a C++ pointer (ignoring purely sytactical details such as
> >'&', '*', and '->')? What can you do with a 'true' reference that cannot
> >be done with a C++ pointer?
>
> I'm not sure I like the term, but what these other languages (and also
> Lisp and Dylan) have in common is that the pointers are all hidden and
> automatically dereferenced as necessary. For instance, in the Lisp code:
>
> (setf a (list 1 2 3))
> (setf b a)
>
> a and b now reference the same list. If you then do
>
> (setf (car a) 4)
> (print (car b))
>
> 4 will be printed. In C or C++, you would have to use explicit pointers
> to make this happen. In C++ you could make b a reference to a, but then
> you run into the fact that you can't change what a reference refers to,
> i.e. you can't do the equivalent of
>
> (setf c (list 7 8 9))
> (setf b c)
>
> In particular, I don't think there's a way with C++ references to do
> something like:
>
> (if (> (car a) (car c))
> (setf b a)
> (setf b c))
>
> (however, I suspect I may be wrong about this, and I'm sure someone will
> leap to show some convoluted way of doing it involving calling a function
> that returns a reference).
No. But I _will_ leap to point out that a counted reference semantics
can be implemented once in a template wrapper class and used wherever
you like. This has some advantages over the other approaches which
are in keeping with the spirit of C++:
1. It is not mandatory. Reference counting and garbage collection
both incur runtime costs. It is exceedingly difficult to design a
real-time GC environment and RC is always slow, especially in a multi-
threaded environment. C++ allows the programmer to revert to "stick
shift" if performance is crucial (which it often is). Other languages
lock you into a protected environment and you have no choice but to
live with the performance hit.
2. It gives the programmer a choice of paradigms. C++ supports both
idioms (or any other idiom, for that matter). You can even mix then
in the one program.
3. It is indirectly supported by the language. In C++, I can pass a
reference counted object like this:
SmartPtr<Database> db;
// ...
f(db);
The fact that C++ doesn't support RC directly provides not the least
syntactic problem. Because RefCount<> is a class, the compiler takes
care of maintaining reference counts through constructors and
destructors. From the programmer's point of view, `db' is just a
variable that gets passed around and knows when to die. You don't
even need to know whether the object is reference counted or garbage
collected (notwithstanding the fact that GC introduces subtly
different semantics).
In short, there aren't too many cats that C++ can't skin! A skim
through Coplien (_Advanced_C++_) should give you an idea of the truly
absurd things C++ can accomplish.
Cheers,
Marcelo
--
______________________________________________________________________
Marcelo Cantos, Research Assistant __/_ marcelo@mds.rmit.edu.au
Multimedia Database Systems Group, RMIT / _ Tel 61-3-9282-2497
L2/723 Swanston St, Carlton VIC 3053, Aus/ralia ><_>Fax 61-3-9282-2490
Acknowledgements: errors - me; wisdom - God; sponsorship - RMIT
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: ark@research.att.com (Andrew Koenig)
Date: 1997/09/10 Raw View
In article <bgu3fvrjx6.fsf@janus.mds.rmit.edu.au> Marcelo Cantos <marcelo@janus.mds.rmit.edu.au> writes:
> I would have thought that `&a = b' was pretty unambiguous.
It is ambiguous if `a' is a reference to a class object that
has operator& defined.
--
--Andrew Koenig
ark@research.att.com
http://www.research.att.com/info/ark
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Steve Clamage <stephen.clamage@Eng.Sun.COM>
Date: 1997/08/28 Raw View
sam varghese wrote:
>
> My undersanding on reading Stroustroup's book is that one cannot
> implement a true primitive reference type in C++. Is this true?
What do you mean by "implementing a true primitive reference
type in C++"? C++ has reference types already, so you
don't need to implement them yourself. It might help if you
gave an example of what you want to do.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: sam varghese <samvarghese@sprintmail.com>
Date: 1997/08/29 Raw View
Steve Clamage wrote:
>
> sam varghese wrote:
> >
> > My undersanding on reading Stroustroup's book is that one cannot
> > implement a true primitive reference type in C++. Is this true?
>
> What do you mean by "implementing a true primitive reference
> type in C++"?
[lots of quoted material edited out. -mod (fjh).]
What I mean by true primitive type is that it has all the properties
of a regular type and yet behaves like a built-in type like pointers and
integers. Reference is a concept built into the C++ language; yet does
it have the same footing as the humble built-in pointer?
If not; can one define a true regular type like the way we define the
STL types like list,map etc?
For example; allocator implementations require operations based on
pointers and references.
Since pointers and references are basically (IMHO) a mechanism to acess
memory ( and possibly have operations defined like iterating over that
memory);
the question remains that is it possible to define a true reference type
that a casual user can use and understand without being bogged down in
subtleties?
My thought for the day:........................
It is possible to be fired from your job by being honest and pleasant.
Ask me!~
Sam :-)
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Tom Payne <thp@cs.ucr.edu>
Date: 1997/08/29 Raw View
In comp.std.c++ sam varghese <samvarghese@sprintmail.com> wrote:
: Steve Clamage wrote:
: >
: > sam varghese wrote:
: > >
: > > My undersanding on reading Stroustroup's book is that one cannot
: > > implement a true primitive reference type in C++. Is this true?
: >
: > What do you mean by "implementing a true primitive reference
: > type in C++"?
: What I mean by true primitive type is that it has all the properties
: of a regular type and yet behaves like a built-in type like pointers and
: integers. Reference is a concept built into the C++ language; yet does
: it have the same footing as the humble built-in pointer?
References in C++ have been hobbled as a deliberate design decision.
They have fixed referents because of Stroustrup's unpleasant
experiences with reseatable references in Algol 68 [D&E, p. 86]. The
ability to build reference types has been restricted (no references to
reference, no arrays of references, etc.) for less well specified
reasons. Behind the hobbling, however, is a semantic paradigm that
holds that reference are "aliases, not objects." (whatever that
means).
: If not; can one define a true regular type like the way we define the
: STL types like list,map etc?
Like a pointer type that gets automatically dereferenced whenever it
is accessed, say via a type conversion operator?
Tom Payne
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]