Topic: What is a variable?
Author: dag.henriksson@quidsoft.se ("Dag Henriksson")
Date: Fri, 9 May 2003 17:20:27 +0000 (UTC) Raw View
"johnchx" <johnchx2@yahoo.com> wrote in message
news:4fb4137d.0305080954.1857853e@posting.google.com...
> [Note: because references are not objects, references cannot be
> created by new-expressions.]
OK. References are *not* objects according to this note.
But according to this, it is *unspecified*:
1.8p1 "An object is a region of storage."
8.3.2p3 "It is unspecified whether or not a reference requires storage
(3.7)."
And according to this, references *are* objects, given that they have a
lifetime:
3.7p4 "...The lifetime of a reference is its storage duration."
3.7p1 "Storage duration is the property of an object"
Not crystal clear to me... Is this a defect in the standard or in my
understanding?
--
Dag Henriksson
---
[ 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: Fri, 9 May 2003 22:42:14 +0000 (UTC) Raw View
dag.henriksson@quidsoft.se ("Dag Henriksson") wrote
> OK. References are *not* objects according to this note.
>
> But according to this, it is *unspecified*:
> 1.8p1 "An object is a region of storage."
> 8.3.2p3 "It is unspecified whether or not a reference requires storage
> (3.7)."
>
While I agree that objects are regions of storage, I'm not sure that
it follows that regions of storage are (all) objects. For example, is
the padding between elements of a struct (for alignment purpose) an
object?
> And according to this, references *are* objects, given that they have a
> lifetime:
> 3.7p4 "...The lifetime of a reference is its storage duration."
> 3.7p1 "Storage duration is the property of an object"
>
Quoting all of 3.7/4 makes the meaning clearer:
The storage duration categories apply to references AS WELL.
The lifetime of a reference is its storage duration.
(emphasis added)
I think it's reasonable to read the "as well" to mean that storage
duration is a property of objects AND references.
> Not crystal clear to me... Is this a defect in the standard or in my
> understanding?
Well, the standard could be clearer, undoubtedly. But I think the
note is pretty convincing evidence that "references aren't objects"
was something the standards committee more or less took for granted.
It convinces me, anyway. :-)
---
[ 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: Thu, 8 May 2003 18:57:54 +0000 (UTC) Raw View
dag.henriksson@quidsoft.se ("Dag Henriksson") wrote
> Even the standard isn't crystal clear here:
>
Actually, it turns out that it is! After a few *days* of skimming the
standard (!) I noticed the following at 5.3.4/1
[Note: because references are not objects, references cannot be
created by new-expressions.]
Alas, it's in a note, but it's nicely unambiguous. Good enough for
me, at any rate. :-)
---
[ 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: Tue, 29 Apr 2003 05:47:35 +0000 (UTC) Raw View
In article <b8c4vr$8b66j$1@fu-berlin.de>, Stefan Ram wrote:
> do-not-spam-ben.hutchings@businesswebsoftware.com (Ben Hutchings) writes:
>>No object needs to have associated storage if you don't take its address.
>
> Every object needs to have associated storage.
>
> If it would not have associated storage, it would not be an
> object.
>
> "An object is a region of storage."
> ISO/IEC 14882:1998(E)
Thanks to the "as-if" rule, though, an implementation doesn't really have
to associate storage with an object if the program doesn't take its address.
That applies as much to objects of int type as it does to objects of pointer
type.
>>Pointers are objects just as much as ints are.
>
> What is "an int" supposed to be?
>
> An int /object/ is an object.
>
> An int /value/ is not (necessarily) an object.
>
> The same holds for pointers.
OK, sloppy wording on my part. What I wanted to say was that pointer types
are "object types", but unfortunately the standard doesn't define such a
term even though it seems to implicitly use the concept (which I understand
as being everything except void, reference types and function types). I'm
having trouble coming up with a wording that is both formally correct and
also not so formal as to require an explanation that is practically
circular.
---
[ 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: Tue, 29 Apr 2003 19:20:16 +0000 (UTC) Raw View
musiphil@bawi.org (KIM Seungbeom) wrote in message
> Does the standard explicitly define the term "variable"?
> The closest that I can find is 3/4, which says:
>
> A /variable/ is introduced by the declaration of an object.
> The variable's name denotes the object.
>
> but it seems not to answer the question "What is a variable?"
>
> My understanding from the C days has been that "a variable is a named
> object," but if it still holds, I cannot explain why 3/3 mentions
> both an object and a variable. I am not sure whether references are
> variables, either.
Putting 3/3 and 3/4 together, I think it's reasonable to say:
A variable is an entity which is introduced by the declaration
of an object and whose name denotes that object.
Perhaps surprisingly, if we accept that definition, then a reference
is not a variable, because it is not introduced by the declaration of
an object:
int i = 5; // i is a variable
int& j = i; // no object declaration, so no variable
(I'm taking for granted here that a reference is not an
object...though others might disagree.)
---
[ 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: dag.henriksson@quidsoft.se ("Dag Henriksson")
Date: Wed, 30 Apr 2003 17:26:32 +0000 (UTC) Raw View
> (I'm taking for granted here that a reference is not an
> object...though others might disagree.)
Even the standard isn't crystal clear here:
1.8p1
"An object is a region of storage."
8.3.2p3
"It is unspecified whether or not a reference requires storage (3.7)."
For me, this means that it's unspecified if a reference is an object.
But if the reference has a lifetime, it must be an object:
3.7p4
"...The lifetime of a reference is its storage duration."
3.7p1
"Storage duration is the property of an object that defines
the minimum potential lifetime of the storage containing
the object."
--
Dag Henriksson
---
[ 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: gdr@integrable-solutions.net (Gabriel Dos Reis)
Date: Wed, 30 Apr 2003 17:29:29 +0000 (UTC) Raw View
johnchx2@yahoo.com (johnchx) writes:
[...]
| Perhaps surprisingly, if we accept that definition, then a reference
| is not a variable,
What would be surprising is that one comes with a definition that
turns a reference into a variable :-)
--
Gabriel Dos Reis, gdr@integrable-solutions.net
---
[ 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: drpizza@anti-flash.co.uk ("DrPizza")
Date: Thu, 24 Apr 2003 18:44:11 +0000 (UTC) Raw View
"KIM Seungbeom" <musiphil@bawi.org> wrote in message
news:bd47bb0e.0304181956.4bd8fdd4@posting.google.com...
> Does the standard explicitly define the term "variable"?
> The closest that I can find is 3/4, which says:
>
> A /variable/ is introduced by the declaration of an object.
> The variable's name denotes the object.
>
> but it seems not to answer the question "What is a variable?"
>
> My understanding from the C days has been that "a variable is a named
> object," but if it still holds, I cannot explain why 3/3 mentions
> both an object and a variable. I am not sure whether references are
> variables, either.
A variable is a name for an object (an object being a lump of memory).
Normally a variable is tightly coupled to the object it refers to; if one
writes:
int i;
then one has no means to divorce "i" from the object it represents.
References and pointers both allow one to create names without creating
corresponding objects. References do so in a one-off manner; once one has
created the reference one cannot make it refer to a different object.
Pointers allow one to make a name refer to different objects (in C and C++
pointers do a little more, as they represent memory addresses, but this is a
minor detail).
A reference isn't a variable; it's /just/ a name. A pointer in practice is a
variable, as it has some associated storage itself, but this is a vagary of C
and C++ pointers.
One needs to differentiate between objects and variables because, just as one
can create names without objects (references), one can can create objects
without names -- anonymous objects.
--
Now Playing: Binary Finary - 2000 (Millennium Mix)
---
[ 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: bop2@telia.com ("Bo Persson")
Date: Fri, 25 Apr 2003 18:04:10 +0000 (UTC) Raw View
""DrPizza"" <drpizza@anti-flash.co.uk> skrev i meddelandet
news:vaf9rlamcqe2a5@corp.supernews.com...
> "KIM Seungbeom" <musiphil@bawi.org> wrote in message
> news:bd47bb0e.0304181956.4bd8fdd4@posting.google.com...
> > Does the standard explicitly define the term "variable"?
> > The closest that I can find is 3/4, which says:
> >
> > A /variable/ is introduced by the declaration of an object.
> > The variable's name denotes the object.
> >
> > but it seems not to answer the question "What is a variable?"
> >
> > My understanding from the C days has been that "a variable is a named
> > object," but if it still holds, I cannot explain why 3/3 mentions
> > both an object and a variable. I am not sure whether references are
> > variables, either.
>
> A variable is a name for an object (an object being a lump of memory).
No, not quite. :-)
A variable *has* a name, just like it can have a value, an adress, etc. It
says above "The variable's name...", not that the name *is* the variable,
but that the variable has a name.
The quote above, with the word "variable" in italics, *is* actually the
definition of the "variable" concept...
Bo "nit pick" Persson
bop2@telia.com
---
[ 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: Fri, 25 Apr 2003 19:15:00 +0000 (UTC) Raw View
In article <b89dpb$7njkv$1@fu-berlin.de>, Stefan Ram wrote:
> drpizza@anti-flash.co.uk ("DrPizza") writes:
<snip>
>>A reference isn't a variable; it's /just/ a name. A pointer in practice is a
>>variable, as it has some associated storage itself, but this is a vagary of C
>>and C++ pointers.
>
> A pointer is a value of a pointer type, like "&c".
> It does not need to have associated storage itself.
No object needs to have associated storage if you don't take its address.
Pointers are objects just as much as ints are.
---
[ 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: Sun, 20 Apr 2003 06:20:12 +0000 (UTC) Raw View
"KIM Seungbeom" <musiphil@bawi.org> wrote...
> Does the standard explicitly define the term "variable"?
It doesn't have to. Basically it's the same as defining "input"
or "algorithm". Those are generic programming terms and the
standard assumes their definition is known to the reader.
> The closest that I can find is 3/4, which says:
>
> A /variable/ is introduced by the declaration of an object.
> The variable's name denotes the object.
>
> but it seems not to answer the question "What is a variable?"
>
> My understanding from the C days has been that "a variable is a named
> object," but if it still holds, I cannot explain why 3/3 mentions
> both an object and a variable.
Could it be because there can be _unnamed_ objects?
> I am not sure whether references are
> variables, either.
References are not objects, so there can be no variable that is
a reference. A reference is an _alias_ for another object.
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: loic.actarus.joly@wanadoo.fr (=?ISO-8859-1?Q?Lo=EFc_Joly?=)
Date: Sun, 20 Apr 2003 23:44:30 +0000 (UTC) Raw View
Stefan Ram wrote:
> After creation, a reference is the same as a variable:
> a pair of a name and an object.
>=20
> After the creation has happened, the reference and the non-
> reference variable can not be distinguished, e.g.:
>=20
> { int a( 0 ); int &b( a );
> /* Try anything here to find a difference
> between "a" and "b" (other than the name). */ }
One difference is that b can be destroyed before a (and a will still=20
behave correctly), whereas a cannot be destroyed before b.
--=20
Lo=EFc
---
[ 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: loic.actarus.joly@wanadoo.fr (=?ISO-8859-1?Q?Lo=EFc_Joly?=)
Date: Mon, 21 Apr 2003 10:49:16 +0000 (UTC) Raw View
Stefan Ram wrote:
> loic.actarus.joly@wanadoo.fr (Lo=EFc Joly) writes:
>=20
>>Stefan Ram wrote:
>>
>>> After the creation has happened, the reference and the non-
>>> reference variable can not be distinguished, e.g.:
>>> { int a( 0 ); int &b( a );
>>> /* Try anything here to find a difference
>>> between "a" and "b" (other than the name). */ }
>>
>>One difference is that b can be destroyed before a (and a will still=20
>>behave correctly), whereas a cannot be destroyed before b.
>=20
>=20
> Could you please insert code in place of the comment
> that shows how to do this?
I cannot do it directly in your code, but I was thinking that in a way,=20
a reference was to a variable the same as a weak pointer to a strong=20
pointer, except that the language enforces that the "weak" cannot be=20
destroyed before the "strong".
{
T a;
{
T &b(a);
// b and a are equivalent
} // b is destroyed, though no destructor is called !
// a is usable, not b
} // a is destroyed, T destructor is called !
--=20
Lo=EFc
---
[ 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 20:50:03 +0000 (UTC) Raw View
Does the standard explicitly define the term "variable"?
The closest that I can find is 3/4, which says:
A /variable/ is introduced by the declaration of an object.
The variable's name denotes the object.
but it seems not to answer the question "What is a variable?"
My understanding from the C days has been that "a variable is a named
object," but if it still holds, I cannot explain why 3/3 mentions
both an object and a variable. I am not sure whether references are
variables, either.
--
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 ]