Revistiing using return value as reference

J

johanatan

If variables can have a 'reference type', then the answer to my
question would be 'yes'. Noticed I asked if an expression could
evaluate to a 'reference to an object'. This is what you seem to be
referring to as a 'variable which happens to be a reference'. But, in
my simplistic world, I just call a variable that happens to be a
reference, or 'has a reference type' as you say, 'references' (and
sometimes if I'm feeling a little verbose, I might say 'reference to
an object'.

Would you say that an object in the real world that has the type of
car is a car? Or does it just have the type of car? This is probably
a philosophical question, but to simple me, it *is* a car.

--Jonathan
 
J

johanatan

Ooops.  Sorry.  Meant this:

int main()
{
  int x = 9;
  f(x);
  g(x);

}

And, actually, my first 'simple' example had a little problem too.
Meant this:

int x;
int& y = x;

but, everyone probably 'extrapolated' that (hopefully truthily (it's a
joke, i don't really think there's such a word)).

--Jonathan
 
A

Alf P. Steinbach

* johanatan:
I think the only way to really say what influenced the C++ reference
would be to ask the designer himself.

I'm pretty sure this is explained in his "evolution" book, although I
don't have that book. You might also look in Bjarne's FAQ. I'm not
sure, but there's a good chance it's covered there too.

Hypothesizing that lang X or
lang Y which had a concept of 'pointer' or 'reference' as the main
motivation for the C++ reference is overlooking the 800 lb. gorilla
(who happens to have almost the same name except for those two plus
signs) and is by anyone's account a *huge* influence. How can a
servant be greater than his master?

Presumably you're talking about C.

The main motivation for C++ references was to support operator
overloading and more generally implementing types and functions on a par
with built-in types and functions.

As Doug McIlroy stated, quoted by Bjarne in TCPPPL, 'These types are not
"abstract"; they are as real as int and float' (e.g. associative array).

Now we use references more to avoid notational clutter and to express
the not-null assumption/requirement -- as so often happens, things
that are useful get used in other ways than originally conceived.

And for that reason we now see weaknesses in the original specification,
e.g., while references made it possible to make new types that behaved
like other built-in types, there's no corresponding mechanism to make
something behave like a reference, e.g. wrt. covariant function results.


Cheers, & hth.,

- Alf
 
J

johanatan

I'm pretty sure this is explained in his "evolution" book, although I
don't have that book.  You might also look in Bjarne's FAQ.  I'm not
sure, but there's a good chance it's covered there too.


Presumably you're talking about C.

Yea, specifically, I was responding to the claim that C++ borrowed the
idea of a reference from the Algol67 or some such language. The more
I think about this discussion, the more it is apparent to me that
these following words are 'synonyms' in the abstract (i.e., aside from
specific implementations):

handle, pointer, address, reference, alias, moniker.

And, I'm sure there are more I'm forgetting right now. This page on
references is quite illuminating:

http://en.wikipedia.org/wiki/Reference_(computer_science)

It says that even in compiled, functional languages, such as Standard
ML and O'Caml, references are usually implemented as 'pointers' under
the hood. And, in Java, C#, and the like, they are implemented as
'two-step pointers' or doubly-indirected pointers.
The main motivation for C++ references was to support operator
overloading and more generally implementing types and functions on a par
with built-in types and functions.

That's interesting. Though, I would still venture to say that the
motivation for the name of the construct of the C++ reference would be
the formal CS literature which the Wiki page I 'referenced' above
summarizes. And, I would think that it would be very hard for someone
already well-versed in C and assembly and most likely the writing of
compilers for both C and C++ to suddenly forget how references could
or even should be implemented. And, also, at this point, if they were
to come up with some different conception of a reference, they would
need to forget the notion of 'call-by-reference' which was very well
established practice in C (and even asm).

The question of which language(s) aside from C 'inspired' the C++
reference is just a red herring. Many languages have a concept of
'reference' and they all get the notion from 'pure' CS literature.
The fact is, he could have chosen any of the above synonyms, but he
didn't, he chose a 'loaded' word which would immediately conjure up
images from other contexts (and also, it just so happens to be the
word that is the most general for this concept in the pure CS
literature). It is the word that is least attached to a specific
implementation. So, to summarize, he could have chosen it for 2
reasons (and I suspect both):

1 - C and assembly connotations
2 - purest name of a CS concept that can be described by a number of
competing names all of which carry more specific connotations (or did
at the time C++ was written)

The name pointer was taken directly from C, and the new construct was
the first time a new name had to be given to that concept in C++ (at
least we don't have 3 types of references, pointers, aliases, do
we?). It makes most sense to go with 'purest CS' and the added C
connotations are just a bonus since C++ programmers should already be
well familiar with C and assembly.

That's my 'constructed' historical view. It that's someone
inaccurate, please do shed light.

--Jonathan
 
J

johanatan

Yea, specifically, I was responding to the claim that C++ borrowed the
idea of a reference from the Algol67 or some such language.  The more
I think about this  discussion, the more it is apparent to me that
these following words are 'synonyms' in the abstract (i.e., aside from
specific implementations):

handle, pointer, address, reference, alias, moniker.

And, I'm sure there are more I'm forgetting right now.  This page on
references is quite illuminating:

http://en.wikipedia.org/wiki/Reference_(computer_science)

It says that even in compiled, functional languages, such as Standard
ML and O'Caml, references are usually implemented as 'pointers' under
the hood.  And, in Java, C#, and the like, they are implemented as
'two-step pointers' or doubly-indirected pointers.


That's interesting.  Though, I would still venture to say that the
motivation for the name of the construct of the C++ reference would be
the formal CS literature which the Wiki page I 'referenced' above
summarizes.  And, I would think that it would be very hard for someone
already well-versed in C and assembly and most likely the writing of
compilers for both C and C++ to suddenly forget how references could
or even should be implemented.  And, also, at this point, if they were
to come up with some different conception of a reference, they would
need to forget the notion of 'call-by-reference' which was very well
established practice in C (and even asm).

The question of which language(s) aside from C 'inspired' the C++
reference is just a red herring.  Many languages have a concept of
'reference' and they all get the notion from 'pure' CS literature.
The fact is, he could have chosen any of the above synonyms, but he
didn't, he chose a 'loaded' word which would immediately conjure up
images from other contexts (and also, it just so happens to be the
word that is the most general for this concept in the pure CS
literature).  It is the word that is least attached to a specific
implementation.  So, to summarize, he could have chosen it for 2
reasons (and I suspect both):

1 - C and assembly connotations
2 - purest name of a CS concept that can be described by a number of
competing names all of which carry more specific connotations (or did
at the time C++ was written)

The name pointer was taken directly from C, and the new construct was
the first time a new name had to be given to that concept in C++ (at
least we don't have 3 types of references, pointers, aliases, do
we?).  It makes most sense to go with 'purest CS' and the added C
connotations are just a bonus since C++ programmers should already be
well familiar with C and assembly.

That's my 'constructed' historical view.  It that's someone
inaccurate, please do shed light.

P.S. I would also add that the 'pure CS' literature probably borrowed
the idea from philosophy (Plato, Aristotle or some such) so if we
really want to take this debate much further, we probably need to
change groups.
 
B

Bo Persson

johanatan said:
The name pointer was taken directly from C, and the new construct
was the first time a new name had to be given to that concept in
C++ (at least we don't have 3 types of references, pointers,
aliases, do we?). It makes most sense to go with 'purest CS' and
the added C connotations are just a bonus since C++ programmers
should already be well familiar with C and assembly.

That's my 'constructed' historical view. It that's someone
inaccurate, please do shed light.

Yes, you are inventing history here.

The names 'class' and 'reference' are taken from the Simula language,
which was around well before C was invented. Like many scandinavians,
Bjarne used that language for his under graduate CS courses.


http://www.research.att.com/~bs/bs_faq.html#why


Bo Persson
 
J

johanatan

Yes, you are inventing history here.

The names 'class' and 'reference' are taken from the Simula language,
which was around well before C was invented. Like many scandinavians,
Bjarne used that language for his under graduate CS courses.

http://www.research.att.com/~bs/bs_faq.html#why

Bo Persson

I'd suggest you read some philosophy, set theory, mathematics and
such. The names 'class' and 'reference' most surely come from those
'purely' intellectual pursuits (aside from any engineering paradigm
including but not limited to any specific computer language).

--Jonathan
 
J

jkherciueh

johanatan said:
I'd suggest you read some philosophy, set theory, mathematics and
such. The names 'class' and 'reference' most surely come from those
'purely' intellectual pursuits (aside from any engineering paradigm
including but not limited to any specific computer language).

You made a very specific conjecture on how those terms entered C++. That
conjecture was disputed. Now you modify your statement (essentially just by
making it vague) so that you only claim the names "came from" philosophy,
set theory, mathematics and such. Well, maybe (although, I would think that
those terms already existed before philosophers, mathematicians and such
picked them up); but that does not account for how they ended up being used
in the standard for C++.

Besides, could you remind me what the intellectual merit of this debate is
supposed to be? It appears that you are not interested in the actual
history (chain of events, considerations, and changes) of the C++ language.
Instead you are developing a "'constructed' historical view". I have to
wonder what the point of such a view is. Apparently, it is a type of
historical account that does not need to be changed even if actual evidence
is presented that it does not tell what actually happened. Now, that need
not make such a fake history useless. It could be some way of explaining
the meanings and inter-relations of certain concepts by supplying a
narrative (I guess Hegel could be read as, in part, doing something like
that). However, in this particular case, I fail to see the point.


Best

Kai-Uwe Bux
 
J

johanatan

Yes, you are inventing history here.

The names 'class' and 'reference' are taken from the Simula language,
which was around well before C was invented. Like many scandinavians,
Bjarne used that language for his under graduate CS courses.

http://www.research.att.com/~bs/bs_faq.html#why

I'm not denying influences of other O-O languages (we could probably
put Algol, Simula, and Smalltalk among others in this list). The
connection I was drawing between C++ references and C's concept of
'call-by-reference' (let alone asm's) was merely provided as support
for the mental image I have of references as pointers. At the very
least, I think I've provided as much rationale for thinking of
references as pointers as anyone has for 'aliases'. (Please see the
wiki page on 'references' if you do not agree with anything I'm saying
about them and also note that the word 'alias' was never mentioned
even once on that page although the obvious understanding of
references as pts 'under the hood' was put forth on multiple
occasions). The alias supporters out there have quite an uphill
battle in front of them it seems (I'm surely not the only one who
'thinks' this way. And who's the thought police anyway?)

Furthermore, if we're going to get really technical about this, a
pointer is a type of reference. The word 'reference' is the most
general word (of a list of many) in common usage to describe the
concept. So, reference swallows pointer, and alias, and moniker, and
handle, and so forth.

And, the concept of 'class' ultimately came from set theory so if C++
didn't directly take the idea from 'pure CS' (or mathematics), then it
did so indirectly as Algol, Smalltalk, Simula and others (whatever the
first O-O languages were) took it from the math literature.

--Jonathan
 
J

johanatan

You made a very specific conjecture on how those terms entered C++. That
conjecture was disputed.

Well, the conjecture was two-pronged from the outset. I said that it
likely was chosen because that's the pure term used in mathematics,
philosophy, and 'pure CS' literature. By 'pure CS' I mean, absent of
any specific technological influence (or as much absent as can be
expected). And, it was just a bonus that it happened to coincide with
the notion of 'call-by-reference' in asm and C.
Now you modify your statement (essentially just by
making it vague) so that you only claim the names "came from" philosophy,
set theory, mathematics and such. Well, maybe (although, I would think that
those terms already existed before philosophers, mathematicians and such
picked them up); but that does not account for how they ended up being used
in the standard for C++.

Yes, I agree. There's definitely value to understanding Algol, Simula
and others' influence, but that's a bit too 'micro' for my needs.
It's good to know, but somewhat beside the point.
Besides, could you remind me what the intellectual merit of this debate is
supposed to be? It appears that you are not interested in the actual
history (chain of events, considerations, and changes) of the C++ language..
Instead you are developing a "'constructed' historical view". I have to
wonder what the point of such a view is. Apparently, it is a type of
historical account that does not need to be changed even if actual evidence
is presented that it does not tell what actually happened. Now, that need
not make such a fake history useless. It could be some way of explaining
the meanings and inter-relations of certain concepts by supplying a
narrative (I guess Hegel could be read as, in part, doing something like
that). However, in this particular case, I fail to see the point.

The point was simply provided as justification for the mental model I
have of reference as pointers 'under the hood'. The specific direct
influence(s) for the concept or term 'reference' in C++ just isn't
that important to me. And, I still fail to see how our two
'histories' are not compatible-- it seems that the Algo and Simula
points are just adding more detail to an already sufficient (at least
to me) narrative.

--Jonathan
 
E

Erik Wikström

I'm not denying influences of other O-O languages (we could probably
put Algol, Simula, and Smalltalk among others in this list). The
connection I was drawing between C++ references and C's concept of
'call-by-reference' (let alone asm's) was merely provided as support
for the mental image I have of references as pointers. At the very
least, I think I've provided as much rationale for thinking of
references as pointers as anyone has for 'aliases'. (Please see the
wiki page on 'references' if you do not agree with anything I'm saying

And we all know that Wikipedia is a fantastically reliable reference.
Since there happens to be at the very least 3 different pages about
references on Wikipedia I assume that you meant the page about C++
references.
about them and also note that the word 'alias' was never mentioned
even once on that page although the obvious understanding of
references as pts 'under the hood' was put forth on multiple
occasions).

A Wikipedia article on the subject does not use the word alias, so what?
If that somehow strengthens your argument then the fact that Stroustrup
uses the word alias in TC++PL should be quite devastating. And by the
way, the word alias was used when I check (and no, I did not edit the page).
The alias supporters out there have quite an uphill
battle in front of them it seems (I'm surely not the only one who
'thinks' this way. And who's the thought police anyway?)

What battle? What makes you think that there is any battle going on?
Furthermore, if we're going to get really technical about this, a
pointer is a type of reference. The word 'reference' is the most
general word (of a list of many) in common usage to describe the
concept. So, reference swallows pointer, and alias, and moniker, and
handle, and so forth.

That depends on what you mean when you use the work reference, if you
use it as it is used in normal talk then yes. However if you by
reference mean a C++ reference then obviously no.
And, the concept of 'class' ultimately came from set theory so if C++
didn't directly take the idea from 'pure CS' (or mathematics), then it
did so indirectly as Algol, Smalltalk, Simula and others (whatever the
first O-O languages were) took it from the math literature.

Oh please, and where do you think the mathematicians got the word from?
The word class have been used to describe classes long before set theory
was invented. The word concept of a class in C++ was named so because
that was the way it was called in earlier languages, they used the word
because it described a similar the concept in math, which used the name
because it described a similar concept in normal speech. Perhaps I
missed some steps, but only the fist is really of interest.
 
D

Daniel T.

johanatan said:
The point was simply provided as justification for the mental model I
have of reference as pointers 'under the hood'.

Here's justification for you...

The obvious implementation of a reference is as a (constant) pointer
that is dereferenced each time it is used. It doesn't do much harm
thinking about references that way, as long as one remembers that a
reference isn't an object that can be manipulate the way a pointer
is...

In some cases, the compiler can optimize away a reference so that
there is no object representing the reference at run-time.
--Stroustrup TC++PL

It seems to me that references are necessary for operator overloading
and copy construction, but otherwise constant pointers could conceivably
be used instead. Stroustrup notes that "The main use of references is
for specifying arguments and return values for functions..." and I tend
to use them in those contexts only, as an optimization over passing by
value.
 
J

johanatan

And we all know that Wikipedia is a fantastically reliable reference.
Since there happens to be at the very least 3 different pages about
references on Wikipedia I assume that you meant the page about C++
references.

No, I meant this one. The one I pasted previously to support my
position (and if you read that entire post, maybe 3 or 4 back, you
might just understand my position.

http://en.wikipedia.org/wiki/Reference_(computer_science)
A Wikipedia article on the subject does not use the word alias, so what?
If that somehow strengthens your argument then the fact that Stroustrup
uses the word alias in TC++PL should be quite devastating. And by the
way, the word alias was used when I check (and no, I did not edit the page).

I think the point is that the 'pure CS' literature has a definition of
a general concept called a 'reference'. Please read the wiki page.
And, if wiki isn't authoritative enough, then go back to the CS
textbooks.
What battle? What makes you think that there is any battle going on?

Well, I was corrected very near the beginning of this post for
thinking of references as pointers. And, the Stanford link I sent had
quite a bias towards 'black-box' view of references (and even went so
far as to say that students 'never need to worry about the details'.
As I mentioned, I would have expected this much from a Java school,
but from a C++ school?
That depends on what you mean when you use the work reference, if you
use it as it is used in normal talk then yes. However if you by
reference mean a C++ reference then obviously no.

You really need to read the wiki page I referenced. (And please
realize it is simply a summary of the concept of 'reference' in CS).
I bet it even has 'references' at the bottom of the page if you think
it was produced by people with an agenda.
Oh please, and where do you think the mathematicians got the word from?

Philosophy. That's why I mentioned Plato and Aristotle too.
The word class have been used to describe classes long before set theory
was invented. The word concept of a class in C++ was named so because
that was the way it was called in earlier languages, they used the word
because it described a similar the concept in math, which used the name
because it described a similar concept in normal speech. Perhaps I
missed some steps, but only the fist is really of interest.

Yea, my point exactly. Even if C++ got the name from Algol or Simula,
those most likely and maybe even indirectly got the name from set
theory who in turn probably got it from philosophy.

I suppose which of those steps (or influences) are of interest depends
entirely on how abstract you want to think.

--Jonathan
 
J

johanatan

johanatan said:
I think the only way to really say what influenced the C++ reference
would be to ask the designer himself.

No need, he published his reason for introducing the concept
(http://www.research.att.com/~bs/dne.html.)

The reference was introduce to facilitate operator overloading. [full
stop]

I mean the influence of the name 'reference'. Is it as I suspect
because that is the purest term in CS literature (and also because
other languages had a concept of 'pass-by-reference')?

Operator overloading only explains the influence for introducing the
construct (or motivation for inventing it)--but not for naming it.

--Jonathan
 
J

johanatan

Here's justification for you...

   The obvious implementation of a reference is as a (constant) pointer
   that is dereferenced each time it is used. It doesn't do much harm
   thinking about references that way, as long as one remembers that a
   reference isn't an object that can be manipulate the way a pointer
   is...

   In some cases, the compiler can optimize away a reference so that
   there is no object representing the reference at run-time.
   --Stroustrup TC++PL

It seems to me that references are necessary for operator overloading
and copy construction, but otherwise constant pointers could conceivably
be used instead.

They surely could, but part of the point with references (at least in
practice) is the 'syntactic sugar'.
Stroustrup notes that "The main use of references is
for specifying arguments and return values for functions..." and I tend
to use them in those contexts only, as an optimization over passing by
value.

Yea, me too (but when I need to overload an operator, then I do so).
If Stroustrup says there's not much harm in thinking of them this way,
then how can anyone else say there is? I actually think there's no
harm because when in doubt, I look at the disassembly and in the
abstract, I've given plenty sufficient reasoning why this mental image
is perfectly acceptable.

But, let's suppose for a minute that the mental image were somehow
incorrect (the determination of which would obviously be somewhat
subjective) but in practice, it produced no discernible difference to
that which would be produced by the ideal mental image (assuming that
there is such a thing--see below for why there is not). In that case
would it really matter that the two mental models differed?

Besides that, an abstraction by definition ignores some of the
details. So there really is no such thing as a perfect abstraction.
Any two given abstractions are going to be less than ideal-- the only
way they differ is in what specific ways they are less than ideal (or
in what specific information they each lose).

--Jonathan
 
J

James Kanze

Well, given that the 'variables' in Java (which are not values, or
intrinsics) are 'references' (usually to objects), I'd say I'm a
little confused by your remark. Would you mind to please clarify?

Just what don't you understand? An expression in Java never
evaluates to an object. An expression is always either a
reference or a basic types.
 
E

Erik Wikström

Well, I was corrected very near the beginning of this post for
thinking of references as pointers. And, the Stanford link I sent had
quite a bias towards 'black-box' view of references (and even went so
far as to say that students 'never need to worry about the details'.
As I mentioned, I would have expected this much from a Java school,
but from a C++ school?

Because it gives an incomplete and somewhat incorrect picture. If you
think of them as pointers you might get the idea that you can perform
pointer arithmetic operations on them, or that they can be null. The
fact that references can be implemented in the same way as a pointer is
an implementation detail, and users really should not have to bother
about that.

Thinking of references as aliases is not perfect either, but it have
some advantages, it is not incorrect (merely incomplete), and does not
bother with implementation details. For someone new to C++ thinking
about references as aliases is better than thinking of them as crippled
pointers, until the time comes that they start to understand what
references really are.
You really need to read the wiki page I referenced. (And please
realize it is simply a summary of the concept of 'reference' in CS).
I bet it even has 'references' at the bottom of the page if you think
it was produced by people with an agenda.

No I do not. My point was that you claim that a pointer is a reference,
I said that depends on what you mean with reference. A C++ pointer is
not a C++ reference, a C++ pointer is a kind of CS reference and a C++
reference is a kind of CS reference. And a CS reference is a kind of
everyday speech reference. Since we are in a C++ group, when you use the
word reference unqualified we will usually assume that you mean a C++
reference.
Philosophy. That's why I mentioned Plato and Aristotle too.

Who in turn got it from?
Yea, my point exactly. Even if C++ got the name from Algol or Simula,
those most likely and maybe even indirectly got the name from set
theory who in turn probably got it from philosophy.

Who in turn got it from?
I suppose which of those steps (or influences) are of interest depends
entirely on how abstract you want to think.

Right, but I believe that the discussion was about where Stroustrup got
the name, and then only the first step is of interest.
 
D

Daniel T.

Erik Wikström said:
Thinking of references as aliases is not perfect either, but it have
some advantages, it is not incorrect (merely incomplete), and does not
bother with implementation details. For someone new to C++ thinking
about references as aliases is better than thinking of them as crippled
pointers, until the time comes that they start to understand what
references really are.

Funny though, at some point in their education. Someone new to C++ will
realize that references _are_ "crippled pointers" (that are
automatically dereferenced) and then comes the inevitable question of
why they are in the language. Then come these almost inevitable
discussions... :)

Maybe if it was explained at the outset, that references are "crippled
pointers", useful precisely because they are "less powerful" (and
therefor more focused to a task,) people wouldn't get confused when they
come to the realization themselves.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,786
Latest member
EmilioGuru

Latest Threads

Top