Topic: Are references objects or not?
Author: dhruvbird@gmx.net (Dhruv)
Date: Sun, 25 May 2003 14:22:00 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) wrote in message news:<bd47bb0e.0305140021.111e96c2@posting.google.com>...
> musiphil@bawi.org (KIM Seungbeom) wrote in message news:<bd47bb0e.0304181946.7eed5ff9@posting.google.com>...
> > I remember once hearing that references are not objects; is it right?
>
Well, references have caught me off guard and I have done some of the
stupidest things with them. I'd better not tell...... :-) Even I have
thinking about this and have come to some pretty satisfactory
conclusions (these are just my conclusions, and may be wrong).
1. References are the 'land of the unknown' because they may behave
differently in different cases.
2. Consider this: int& x = int_var;
Now, the compiler may/may not keep space for this
reference. It may be implemented as a pointer that is dereferenced on
use, or it just acts like an alias.
3. const int& x = 5;
Here, a temporary is created, and using x means using that
temporary. Ofcource, x cannot be used as an lvalue.
4. References as function arguments
a. Non-inline functions.
Here, a reference argument maye be implemented as a
dereference-on-use kind of pointer, or the compiler may produce
amazing optimizations which even I do not know of (such as if the
argument is a const reference, then the argument is never passed, and
at compile time the constant is inserted wherever it is used).
b. Inline functions.
I presume that for inline functions the references as
well as value parameters vanish completely (ok, value parameters
vanish only if they are declared as const, or the smart optimizing
compiler detects no change of value to that parameter). The reference
parameters for inline functions are a good thing, it seems.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: johnmadsen_usenet@hotmail.com (John Madsen)
Date: Tue, 27 May 2003 17:13:25 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) wrote in message news:<bd47bb0e.0305140021.111e96c2@posting.google.com>...
> In this thread and another one in this group, I see many people
> agree that references are not objects. But consider:
>
> >
> > Moreover, a reference has a name, and a name is defined to denote an
> > entity or label(3/4). Since it is obviously not a label, it should be
> > an entity. 3/3 states:
> >
> > An entity is a value, object, subobject, base class subobject,
> > array element, variable, function, instance of a function,
> > enumerator, type, class member, template, or namespace.
> >
> > It does not mention references, which implies a reference is one of those.
>
Could you point out where in the standard it says that a reference has
a name. All I can find is a note in 8.3.2/1 which says "a reference
can be thought of as a name of an object." However, note that having
a name need not be the same thing as being named. For example, an
unnamed list of names has lots of names, but is itself, by definition,
unnamed.
> Since references are not objects, which of the following is false?
>
> (a) A reference has a name.
> (b) A name denotes an entity or label.
> (c) A reference is not a label.
> Therefore
> (d) A reference denotes an entity.
>
> If (d) is true, which of the following is a reference?
>
> An entity is a value, object, subobject, base class subobject,
> array element, variable, function, instance of a function,
> enumerator, type, class member, template, or namespace.
(a)-(d) could all be true and the answer to your question be none of
them. The assumption you appear to be making is that there is some
name that could denote a reference. This is not the case. The
clincher seems to me to be the fact that the standard explicitly
forbids references to references (8.3.2/4).
John
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Mon, 19 May 2003 19:50:07 +0000 (UTC) Raw View
"William Hayes" <whayes@hfx.andara.com> wrote...
> ""Victor Bazarov"" <v.Abazarov@attAbi.com> wrote in message
> news:vc51egpide3j45@corp.supernews.com...
> > "KIM Seungbeom" <musiphil@bawi.org> wrote...
> > > musiphil@bawi.org (KIM Seungbeom) wrote in message
> > news:<bd47bb0e.0304181946.7eed5ff9@posting.google.com>...
> > > > I remember once hearing that references are not objects; is it
right?
> > >
> > > In this thread and another one in this group, I see many people
> > > agree that references are not objects. But consider:
> > >
> > > >
> > > > Moreover, a reference has a name, and a name is defined to denote an
> > > > entity or label(3/4). Since it is obviously not a label, it should
be
> > > > an entity. 3/3 states:
> > > >
> > > > An entity is a value, object, subobject, base class subobject,
> > > > array element, variable, function, instance of a function,
> > > > enumerator, type, class member, template, or namespace.
> > > >
> > > > It does not mention references, which implies a reference is one of
> > those.
> > >
> > > Since references are not objects, which of the following is false?
> > >
> > > (a) A reference has a name.
> > > (b) A name denotes an entity or label.
> > > (c) A reference is not a label.
> > > Therefore
> > > (d) A reference denotes an entity.
> >
> > Incorrect. A reference would denote an entity if it _were_
> > a name. The statement (a) says that a reference _has_ a name,
> > not that it _is_ a name.
> >
> > >
> > > If (d) is true, which of the following is a reference?
> > >
> > > An entity is a value, object, subobject, base class subobject,
> > > array element, variable, function, instance of a function,
> > > enumerator, type, class member, template, or namespace.
> >
> > A reference can be a value, it can be a variable, it can be
> > a class member. The concept of a reference is orthogonal to
> > the concept of "entity", not "in line" or "opposite" to it.
> >
> > Victor
> > --
>
> References appear to have two forms.
>
> In the case of a reference declared in global or block scope, as in r
below,
> I think it is just a name:
>
> int o;
> int& r = o;
>
> The object to which r is bound is the same one to which o is bound. To
make
> it more concrete, assume that the object is a global stored at location
> 1000. The compiler presumably maintains a table that associates the
symbol
> 'o' and the symbol 'r' with the address 1000. At runtime, the symbols no
> longer need to exist.
>
> In other cases, however, references must be represented as objects. If
you
> pass a reference as a function parameter, for example, it would seem that
it
> must be passed as an object. Presumably when r is passed to a function
> expecting a reference-to-int, the value 1000 is passed, just as it would
be
> if the parameter were a pointer-to-int.
>
> I'm not sure that there is a simple conceptual model that we can invoke
when
> using references...
So, combining the two examples you gave:
int o;
int & ro = o;
int & rro = ro; // what's happening here?
IMO, when function arguments are concerned, you're not passing
_a_reference_, you're passing an object _by_reference_. And it
is the same as defining a reference and initialising it _from_
another reference but _with_ an object. So, it becomes just
another name for the object. That's my understanding. And BTW
you can see that there are two argument passing schemes in C++:
by value and by reference. If references were treated as objects
when used as function arguments, we would not have "by reference",
we would only have "by value", so the references would have values
and that would mean they require storage, etc.
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: whayes@hfx.andara.com ("William Hayes")
Date: Wed, 21 May 2003 19:27:04 +0000 (UTC) Raw View
""Victor Bazarov"" <v.Abazarov@attAbi.com> wrote in message
news:vci2dl420uk80a@corp.supernews.com...
> So, combining the two examples you gave:
>
> int o;
> int & ro = o;
> int & rro = ro; // what's happening here?
>
> IMO, when function arguments are concerned, you're not passing
> _a_reference_, you're passing an object _by_reference_. And it
> is the same as defining a reference and initialising it _from_
> another reference but _with_ an object. So, it becomes just
> another name for the object. That's my understanding. And BTW
> you can see that there are two argument passing schemes in C++:
> by value and by reference. If references were treated as objects
> when used as function arguments, we would not have "by reference",
> we would only have "by value", so the references would have values
> and that would mean they require storage, etc.
>
> Victor
> --
> Please remove capital A's from my address when replying by mail
I agree that references, even when function parameters, can be looked upon
as another name for an object, and probably that is how we should view them.
My point was that the way references are probably implemented does sometimes
involve storage, and this fact leads to confusion.
One could argue that we should never think about implementation details of
this kind when trying to understand a language, but sometimes the question
of how something is actually implemented leaps out at you.
Regarding the two argument passing schemes: I was talking about
implementation. I am frankly ignorant of how pass-by-reference is typically
implemented, but my guess is that in most implementations a value is passed,
namely the address of the object, and it does require storage -- it ends up
in the stack frame with the rest of the parameters. It's like a constant
pointer parameter, but with different semantics when it's used inside the
function. It's the different semantics that make it pass-by-reference from
the programmer's point of view, instead of pass-by-value as it would be in
the case of a pointer. Again, this is pure speculation on my part.
However, that doesn't mean that we can't think of the parameter as a simple
alternate name for the object that was passed. It acts like one, and that
may be all we need to know.
Bill
-----------------------------------
William Hayes
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: do-not-spam-ben.hutchings@businesswebsoftware.com (Ben Hutchings)
Date: Wed, 14 May 2003 19:22:33 +0000 (UTC) Raw View
In article <bd47bb0e.0305140021.111e96c2@posting.google.com>,
KIM Seungbeom wrote:
<snip>
> Since references are not objects, which of the following is false?
>
> (a) A reference has a name.
I think you meant to say that a reference *is* a name, because
otherwise the conclusion does not follow.
> (b) A name denotes an entity or label.
> (c) A reference is not a label.
> Therefore
> (d) A reference denotes an entity.
>
> If (d) is true, which of the following is a reference?
>
> An entity is a value, object, subobject, base class subobject,
> array element, variable, function, instance of a function,
> enumerator, type, class member, template, or namespace.
None, because 'denotes' does not mean 'is'.
Yes, a reference denotes some entity - the object, sub-object or
function that it is bound to when initialised.
An object has an address and a size (defining a region of storage),
which you can find these with the sizeof and unary & operators
(assuming that the latter hasn't been overloaded). References have
neither of these attributes (in the type model).
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Wed, 14 May 2003 19:48:27 +0000 (UTC) Raw View
"KIM Seungbeom" <musiphil@bawi.org> wrote...
> musiphil@bawi.org (KIM Seungbeom) wrote in message
news:<bd47bb0e.0304181946.7eed5ff9@posting.google.com>...
> > I remember once hearing that references are not objects; is it right?
>
> In this thread and another one in this group, I see many people
> agree that references are not objects. But consider:
>
> >
> > Moreover, a reference has a name, and a name is defined to denote an
> > entity or label(3/4). Since it is obviously not a label, it should be
> > an entity. 3/3 states:
> >
> > An entity is a value, object, subobject, base class subobject,
> > array element, variable, function, instance of a function,
> > enumerator, type, class member, template, or namespace.
> >
> > It does not mention references, which implies a reference is one of
those.
>
> Since references are not objects, which of the following is false?
>
> (a) A reference has a name.
> (b) A name denotes an entity or label.
> (c) A reference is not a label.
> Therefore
> (d) A reference denotes an entity.
Incorrect. A reference would denote an entity if it _were_
a name. The statement (a) says that a reference _has_ a name,
not that it _is_ a name.
>
> If (d) is true, which of the following is a reference?
>
> An entity is a value, object, subobject, base class subobject,
> array element, variable, function, instance of a function,
> enumerator, type, class member, template, or namespace.
A reference can be a value, it can be a variable, it can be
a class member. The concept of a reference is orthogonal to
the concept of "entity", not "in line" or "opposite" to it.
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: kanze@gabi-soft.de (James Kanze)
Date: Thu, 15 May 2003 21:02:42 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) wrote in message
news:<bd47bb0e.0305140021.111e96c2@posting.google.com>...
> musiphil@bawi.org (KIM Seungbeom) wrote in message
> news:<bd47bb0e.0304181946.7eed5ff9@posting.google.com>...
> > I remember once hearing that references are not objects; is it
> > right?
> In this thread and another one in this group, I see many people
> agree that references are not objects. But consider:
> > Moreover, a reference has a name, and a name is defined to denote an
> > entity or label(3/4). Since it is obviously not a label, it should be
> > an entity. 3/3 states:
> > An entity is a value, object, subobject, base class subobject,
> > array element, variable, function, instance of a function,
> > enumerator, type, class member, template, or namespace.
> > It does not mention references, which implies a reference is one of those.
> Since references are not objects, which of the following is false?
> (a) A reference has a name.
False. A reference defines a name, but the name refers to what is
referenced, and not the reference.
> (b) A name denotes an entity or label.
> (c) A reference is not a label.
> Therefore
> (d) A reference denotes an entity.
> If (d) is true, which of the following is a reference?
> An entity is a value, object, subobject, base class subobject,
> array element, variable, function, instance of a function,
> enumerator, type, class member, template, or namespace.
You've changed the verb again. A reference *denotes* an object or a
function. A reference *is* a reference. It may have a name. But (with
or without a name) it is a way of denoting an object or a function.
The important thing to realise is that you simply cannot refer to a
reference in a program, other than in its declaration. As such, it
cannot be an object.
--
James Kanze GABI Software mailto:kanze@gabi-soft.fr
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, T l. : +33 (0)1 30 23 45 16
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: whayes@hfx.andara.com ("William Hayes")
Date: Sun, 18 May 2003 02:34:39 +0000 (UTC) Raw View
""Victor Bazarov"" <v.Abazarov@attAbi.com> wrote in message
news:vc51egpide3j45@corp.supernews.com...
> "KIM Seungbeom" <musiphil@bawi.org> wrote...
> > musiphil@bawi.org (KIM Seungbeom) wrote in message
> news:<bd47bb0e.0304181946.7eed5ff9@posting.google.com>...
> > > I remember once hearing that references are not objects; is it right?
> >
> > In this thread and another one in this group, I see many people
> > agree that references are not objects. But consider:
> >
> > >
> > > Moreover, a reference has a name, and a name is defined to denote an
> > > entity or label(3/4). Since it is obviously not a label, it should be
> > > an entity. 3/3 states:
> > >
> > > An entity is a value, object, subobject, base class subobject,
> > > array element, variable, function, instance of a function,
> > > enumerator, type, class member, template, or namespace.
> > >
> > > It does not mention references, which implies a reference is one of
> those.
> >
> > Since references are not objects, which of the following is false?
> >
> > (a) A reference has a name.
> > (b) A name denotes an entity or label.
> > (c) A reference is not a label.
> > Therefore
> > (d) A reference denotes an entity.
>
> Incorrect. A reference would denote an entity if it _were_
> a name. The statement (a) says that a reference _has_ a name,
> not that it _is_ a name.
>
> >
> > If (d) is true, which of the following is a reference?
> >
> > An entity is a value, object, subobject, base class subobject,
> > array element, variable, function, instance of a function,
> > enumerator, type, class member, template, or namespace.
>
> A reference can be a value, it can be a variable, it can be
> a class member. The concept of a reference is orthogonal to
> the concept of "entity", not "in line" or "opposite" to it.
>
> Victor
> --
References appear to have two forms.
In the case of a reference declared in global or block scope, as in r below,
I think it is just a name:
int o;
int& r = o;
The object to which r is bound is the same one to which o is bound. To make
it more concrete, assume that the object is a global stored at location
1000. The compiler presumably maintains a table that associates the symbol
'o' and the symbol 'r' with the address 1000. At runtime, the symbols no
longer need to exist.
In other cases, however, references must be represented as objects. If you
pass a reference as a function parameter, for example, it would seem that it
must be passed as an object. Presumably when r is passed to a function
expecting a reference-to-int, the value 1000 is passed, just as it would be
if the parameter were a pointer-to-int.
I'm not sure that there is a simple conceptual model that we can invoke when
using references...
-----------------------------------
William Hayes
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: musiphil@bawi.org (KIM Seungbeom)
Date: Wed, 14 May 2003 17:22:11 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) wrote in message news:<bd47bb0e.0304181946.7eed5ff9@posting.google.com>...
> I remember once hearing that references are not objects; is it right?
In this thread and another one in this group, I see many people
agree that references are not objects. But consider:
>
> Moreover, a reference has a name, and a name is defined to denote an
> entity or label(3/4). Since it is obviously not a label, it should be
> an entity. 3/3 states:
>
> An entity is a value, object, subobject, base class subobject,
> array element, variable, function, instance of a function,
> enumerator, type, class member, template, or namespace.
>
> It does not mention references, which implies a reference is one of those.
Since references are not objects, which of the following is false?
(a) A reference has a name.
(b) A name denotes an entity or label.
(c) A reference is not a label.
Therefore
(d) A reference denotes an entity.
If (d) is true, which of the following is a reference?
An entity is a value, object, subobject, base class subobject,
array element, variable, function, instance of a function,
enumerator, type, class member, template, or namespace.
--
KIM Seungbeom <musiphil@bawi.org>
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: johnchx2@yahoo.com (johnchx)
Date: Mon, 21 Apr 2003 19:46:10 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) wrote
> Moreover, a reference has a name, and a name is defined to denote an
> entity or label(3/4). Since it is obviously not a label, it should be
> an entity. 3/3 states:
>
> An entity is a value, object, subobject, base class subobject,
> array element, variable, function, instance of a function,
> enumerator, type, class member, template, or namespace.
>
> It does not mention references, which implies a reference is one of those.
I believe that the name of a reference denotes the object (or
function) to which the reference refers (not the reference itself).
For example:
int i = 5;
int& ri = i;
The name ri denotes the object i (or, put another way, i and ri denote
the same object). Notice, for instance that &i == &ri.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: musiphil@bawi.org (KIM Seungbeom)
Date: Sat, 19 Apr 2003 04:23:55 +0000 (UTC) Raw View
I remember once hearing that references are not objects; is it right?
I cannot find the clause in the standard that gives a definitive answer.
In many clauses (such as 3.5 and 3.9/1) the standard juxtaposes objects
and references, which seems to imply that references are not objects.
3.9/8 even states that an object type is a (possibly cvqualified) type
that is not a function type, not a reference type, and not a void type,
but I'm not sure whether it means that references are not objects.
Moreover, a reference has a name, and a name is defined to denote an
entity or label(3/4). Since it is obviously not a label, it should be
an entity. 3/3 states:
An entity is a value, object, subobject, base class subobject,
array element, variable, function, instance of a function,
enumerator, type, class member, template, or namespace.
It does not mention references, which implies a reference is one of those.
What is the definitive answer?
--
KIM Seungbeom <musiphil@bawi.org>
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Sat, 19 Apr 2003 20:50:24 +0000 (UTC) Raw View
"KIM Seungbeom" <musiphil@bawi.org> wrote...
> I remember once hearing that references are not objects; is it right?
Yes, references are not objects.
> I cannot find the clause in the standard that gives a definitive answer.
It's not normative, but the note in 5.3.4 says "because references
are not objects, ..." Of course, that's no definitive answer, but
8.3.2/3 says that references may not require storage and /4 says
that there are no references to, nor array of, nor pointers to,
references. That's pretty much an indicator that references are
not objects (1.8/1 says that "An object is a region of storage").
> In many clauses (such as 3.5 and 3.9/1) the standard juxtaposes objects
> and references, which seems to imply that references are not objects.
> 3.9/8 even states that an object type is a (possibly cvqualified) type
> that is not a function type, not a reference type, and not a void type,
> but I'm not sure whether it means that references are not objects.
>
> Moreover, a reference has a name, and a name is defined to denote an
> entity or label(3/4). Since it is obviously not a label, it should be
> an entity. 3/3 states:
>
> An entity is a value, object, subobject, base class subobject,
> array element, variable, function, instance of a function,
> enumerator, type, class member, template, or namespace.
>
> It does not mention references, which implies a reference is one of those.
>
> What is the definitive answer?
Depends on what you'd accept as such :-)
Victor
--
Please remove capital A's from my address when replying by mail
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]