Revistiing using return value as reference

J

johanatan

 No, *that* just isn't true (on both technical and conceptual
 levels).  Eric is exactly right: references are aliases for existing
 objects.  Full stop.

If that's true, why is it recommended to pass large objects by
reference? It is to save memory, no? The shortest amount of memory
that you could use to 'alias' an object is a ptr. If you choose
instead to use some kind of opaque reference under the skin (the only
other option), it would still in essence be a 'ptr'. Conceptually, it
certainly 'points' to the object and in practice in every compiler
I've ever seen, this translates to an actual pointer. It would be
quite unorthodox to do something different, though it certainly is
possible.
 No indeed.  Pointers can be reseated, can be null, and take a
 (platform-specific) number of bytes.  References cannot be reseated,
 cannot be "null" (although they can be invalid), and don't have a
 size.

My point was the references are wrappers around pointers
(conceptually) that hide some of the features and add others
(specifically the syntactic sugar). I was in no way suggesting that
you can do everything with a reference that you can do with a pointer
(a fact evidenced by this very discussion about the differences).
 The compiler may *implement* references internally as pointers, but
 that's irrelevent to this discussion.

Actually, I think it's quite relevant. Take a look at some
disassembly sometime if you're not convinced. As I mentioned
previously, a compiler could choose some other form of opaque
reference under the skin, but that would be quite a stretch for it do
so (and it would have to have some very strong rationale for doing so
which at this point is completely theoretical).
 I think they were *supposed* to be conceptually the same in Java and
 in C++, but it didn't really work out that way.


 Which are all things you can do with C++ *pointers*, and cannot be
 done with C++ *references*.  Which is what Eric said, and which I
 reiterated.

Well, the obvious difference between references and pointers is that
the references 'look' like the objects themselves (which is exactly
the way they work in Java), something I have referred to previously as
'syntactic sugar'.
  Under the skin, C++ references are C++ references, period.


  I'd suggest that Java took the idea one step backwards, but that's a
  different debate. :)

Depends on whether you value human time or computer time more I
suppose. As computer time becomes dirt cheap, I think you'll start
valuing human time more.

--Jonathan
 
J

jkherciueh

Dave said:
No, *that* just isn't true (on both technical and conceptual
levels). Eric is exactly right: references are aliases for existing
objects. Full stop.

That is misleading in that it could create the misconception that references
only bind to pre-existing objects (objects that exist independently of the
attempt of binding a reference to them).

When an rvalue of class type is used to initialize a const reference, the
implementation is allowed to make a copy of that object first and then bind
the reference to an object that would not even exist if you had not tried
to bind it to a reference. In that case, the reference is not an alias for
the object used to initialize the reference but for a different object that
even can have different cv-qualifications. This is the reason that, sadly,
the following method to obtain a reference to a non-const temporary is
flawed:

/*
| lvalue_cast( non-const expression ) creates an lvalue out
| of anything, including a temporary. Just make sure, the
| expression is not constant.
*/

template < typename T >
T & lvalue_cast ( const T & ref ) {
return( const_cast< T & >( ref ) );
}


The C++ standard does not say what references are. In particular, it does
not claim references to be aliases. It only defines how they contribute to
the observable behavior of a program. The precise rules are complicated and
cannot be deduced or guessed from the slogan that references are aliases
for objects.


Best

Kai-Uwe Bux
 
E

Erik Wikström

If that's true, why is it recommended to pass large objects by
reference? It is to save memory, no? The shortest amount of memory
that you could use to 'alias' an object is a ptr.

Actually not quite true, since a pointer, as opposed to a reference, is
an object in its own right it must also have an address (take up some
amount of memory). This means that a compiler can in some situation more
easily optimise away references, especially to objects at a know offset
on the stack, where the pointer have to be included.
If you choose
instead to use some kind of opaque reference under the skin (the only
other option), it would still in essence be a 'ptr'. Conceptually, it
certainly 'points' to the object and in practice in every compiler
I've ever seen, this translates to an actual pointer. It would be
quite unorthodox to do something different, though it certainly is
possible.

Quite true that in most cases they are implemented as pointers, but
conceptually (from the way they behave) they are very different from
pointers. And using the same conceptual view a Java reference have more
in common with a C++ pointer than with a reference.
My point was the references are wrappers around pointers
(conceptually) that hide some of the features and add others
(specifically the syntactic sugar). I was in no way suggesting that
you can do everything with a reference that you can do with a pointer
(a fact evidenced by this very discussion about the differences).

That is not conceptually, that is implementation details.
Actually, I think it's quite relevant. Take a look at some
disassembly sometime if you're not convinced. As I mentioned
previously, a compiler could choose some other form of opaque
reference under the skin, but that would be quite a stretch for it do
so (and it would have to have some very strong rationale for doing so
which at this point is completely theoretical).

Actually it is very relevant, the C++ standard only defines how the
different concepts work, not how they are implemented. When comparing
pointers and references it is their behaviour that counts, not how it is
achieved.

Hardly, C++ concepts had been around for some years before Java came to
the scene, the Java references were inspired from some other language.
While I do not remember the genealogy I would suspect Smalltalk or one
of those early OO languages.
Well, the obvious difference between references and pointers is that
the references 'look' like the objects themselves (which is exactly
the way they work in Java), something I have referred to previously as
'syntactic sugar'.

Personally I would call that a superficial difference, sure it is the
most obvious difference for one new to pointers and references but if
that was all it was to it I doubt that someone would have felt the need
to add references in the first place.
 
J

johanatan

Actually not quite true, since a pointer, as opposed to a reference, is
an object in its own right it must also have an address (take up some
amount of memory). This means that a compiler can in some situation more
easily optimise away references, especially to objects at a know offset
on the stack, where the pointer have to be included.

You are talking about compiler optimizations which can be applied
whether a ptr or ref is used. The same optimizations can be applied
equally well to either (similar to inlining it sounds like) and if you
would accept what I'm telling you, a reference is compiled to exactly
the same thing as a ptr.

Suppose you have a ref like this:

int****&

We would agree on the first 4 asterisks, no? Those are definitely
ptrs, right? It's only that last level of indirection that is in
dispute, right? What construct do we have at our disposal (in
assembly, C, C++) to bridge that last level of indirection? I would
highly recommend that if you decide to write a compiler, that you
consider our old friend-- the ptr!
Quite true that in most cases they are implemented as pointers, but
conceptually (from the way they behave) they are very different from
pointers. And using the same conceptual view a Java reference have more
in common with a C++ pointer than with a reference.

Imaging that you knew C (or possibly even assembly) before you learned
C++. How did you pass things by reference? I'll leave the code
example for you to write if you don't see this point.
That is not conceptually, that is implementation details.

When I say conceptual, I mean literally that--a concept in the brain.
Are you telling me that you can't *think* of references as ptrs under
the skin?
Actually it is very relevant, the C++ standard only defines how the
different concepts work, not how they are implemented. When comparing
pointers and references it is their behaviour that counts, not how it is
achieved.

Have you ever programmed in Java or C#? The reference certainly feels
the same whether you are in C++ or Java or C#. The only difference
are the little gotchas that are more plentiful in C++ (i.e., special
cases) due to what is really a handicapped reference (when compared
objectively to the theoretical concept) that in C++ was no more than a
piece of syntactic sugar for hiding a level of indirection (one level,
presumably the last).
Hardly, C++ concepts had been around for some years before Java came to
the scene, the Java references were inspired from some other language.
While I do not remember the genealogy I would suspect Smalltalk or one
of those early OO languages.

Yes, surely. Most CS concepts are not new but were invented in the
60's and 70's and have been repackaged or renamed over time.
Designers of languages are just like the general population in some
senses--susceptible to what is fashionable or 'in style'. I was
theorizing that C++ was merely one implementation of the reference
which was the inspiration for Java purely because of its popularity.
After all, many other langs have had such a concept, but very few
provided so much of the structure of the language of Java as C++.
Surely you see the similarity in syntax between Java and C++?
Personally I would call that a superficial difference, sure it is the
most obvious difference for one new to pointers and references but if
that was all it was to it I doubt that someone would have felt the need
to add references in the first place.

For you, the superficial difference is for me the main feature. I see
all the other gotchas regarding refs in C++ as merely superficial side
effects of various constraints operating in conjunction with one
another (i.e., emergence, in this case-- not very pretty emergence).

I guess we'll just have to agree to disagree on the 'superficiality'
of the various aspects of refs.

--Jonathan
 
J

johanatan

> The C++ standard does not say what references are. In particular, it does
not claim references to be aliases. It only defines how they contribute to
the observable behavior of a program. The precise rules are complicated and
cannot be deduced or guessed from the slogan that references are aliases
for objects.

Yes. Exactly. A little digging around the net turned up this page
(which provides a little bkg info on how the 'alias' notion is being
promoted):

http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references

It seems that the idea of an alias is some academic's way of coming up
with a concept that is easier to understand than 'ptr'. In this page,
he even concedes that to a C programmer, an entity that is passed by
reference is done with a ptr. However, he makes this point:

"References seem to have much in common with pointers, but the actual
workings remain "under
the covers," and the programmer never needs to worry about them."

I highly disagree with the idea that a programmer never need to worry
about anything and can't believe this is coming out of an institution
as prestigious as Stanford. If they want students to not worry about
stuff, then they should just become a 'Java school' (though I disagree
with Java programmers not worrying about stuff too).

See:
http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html

The same logic that Joel applies to 'Java schools' could be applied to
the practice of hiding details from students regarding references in C+
+. In my opinion, (and apparently Joel's too) a programmer that knows
both the black-box view and the white-box view is going to be better
(in all respects) to the one who just knows the black-box view (not
saying that anyone here doesn't have a white-box view, but I just
think that some ppl like to keep conversation flowing) ;-) and
probably don't disagree with me as much as they think they do (or are
representing in this thread).

--Jonathan
 
J

jkherciueh

johanatan said:
Yes. Exactly. A little digging around the net turned up this page
(which provides a little bkg info on how the 'alias' notion is being
promoted):

http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references

It seems that the idea of an alias is some academic's way of coming up
with a concept that is easier to understand than 'ptr'. In this page,
he even concedes that to a C programmer, an entity that is passed by
reference is done with a ptr. However, he makes this point:

"References seem to have much in common with pointers, but the actual
workings remain "under
the covers," and the programmer never needs to worry about them."

I highly disagree with the idea that a programmer never need to worry
about anything and can't believe this is coming out of an institution
as prestigious as Stanford.

Not worrying about anything and not worrying about references about the
inner workings of references are two different things. I am positive that
this professor is pushing the students to worry a lot about many things.

You can worry all you want about the inner workings of references, it won't
do you any good because the standard does not specify those. The standard
does not say what references are nor how they work "under the covers".
Thus, you have no means to address you worries. In that sense, the
professor is just stating a truism. The only programmers who have a
meaningful opportunity to worry about how references work "under the
covers" are those programmers that implement C++ compilers.

All other programmers should be busy enough worrying about the life-time of
objects bound to references and the provisions for initializing references.
None of those are intuitive and none of those are successfully captured in
any of the nutshell-explanations for references. I find that neither the
talk about aliases nor the comparison with pointers is helpful in dealing
with references. There is nothing more (but also nothing less!) to
references than the specs from the standard; and those rules are
complicated and cannot be captured by a simple slogan or comparison.

If they want students to not worry about
stuff, then they should just become a 'Java school' (though I disagree
with Java programmers not worrying about stuff too).
[snip]

This is converging toward drivel and language wars. It does not add anything
meaningful to the technical discussion of references in C++.


Best

Kai-Uwe Bux
 
B

Bo Persson

johanatan wrote:
:: On 2007-12-29 00:14, johanatan wrote:
:
: You are talking about compiler optimizations which can be applied
: whether a ptr or ref is used. The same optimizations can be applied
: equally well to either (similar to inlining it sounds like) and if
: you would accept what I'm telling you, a reference is compiled to
: exactly the same thing as a ptr.

That's an implementation detail.

You wouldn't say that an int and and unsigned int is the same, just
beacuse the bit patterns look the same for the value 42.

:
: Suppose you have a ref like this:
:
: int****&
:
: We would agree on the first 4 asterisks, no? Those are definitely
: ptrs, right? It's only that last level of indirection that is in
: dispute, right? What construct do we have at our disposal (in
: assembly, C, C++) to bridge that last level of indirection? I would
: highly recommend that if you decide to write a compiler, that you
: consider our old friend-- the ptr!

That's an implementation detail. :)

:
: Imaging that you knew C (or possibly even assembly) before you
: learned C++. How did you pass things by reference? I'll leave the
: code example for you to write if you don't see this point.

In C you only have pass by value, so you have to simulate anything
else by taking the address of an object, and passing the resulting
pointer by value.

A reference doesn't point to anything, it refers to an object.

:
::::: Technically, a reference in C++ is just a ptr with syntactic
::::: sugar
::::: to allow using it just like the object itself instead of
::::: dereferencing the ptr (or using the -> operator).
::
:::: No indeed. Pointers can be reseated, can be null, and take a
:::: (platform-specific) number of bytes. References cannot be
:::: reseated, cannot be "null" (although they can be invalid), and
:::: don't have a size.
::
::: My point was the references are wrappers around pointers
::: (conceptually) that hide some of the features and add others
::: (specifically the syntactic sugar). I was in no way suggesting
::: that you can do everything with a reference that you can do with
::: a pointer (a fact evidenced by this very discussion about the
::: differences).
::
:: That is not conceptually, that is implementation details.
::
:
: When I say conceptual, I mean literally that--a concept in the
: brain.

It's also a concept of the language. A programming language is all
about abstractions.

A reference is a totally different abstraction than a pointer. That's
why we have both, and that's why they behave differently.

: Are you telling me that you can't *think* of references as
: ptrs under the skin?

I think he is saying that you shouldn't think of a reference as a
pointer, just because they are implemented similary on some hardware.
Just like you shouldn't think that an int is a long, just becase they
are on some compilers (but not on others).

:
: Yes, surely. Most CS concepts are not new but were invented in the
: 60's and 70's and have been repackaged or renamed over time.
: Designers of languages are just like the general population in some
: senses--susceptible to what is fashionable or 'in style'. I was
: theorizing that C++ was merely one implementation of the reference
: which was the inspiration for Java purely because of its popularity.
: After all, many other langs have had such a concept, but very few
: provided so much of the structure of the language of Java as C++.
: Surely you see the similarity in syntax between Java and C++?

No, Java and C++ just looks similar, because they have borrowed syntax
elements from C. The ideas behind the languages are different. That
makes things that looks similar actually behave differently.

:
::::: -. Java references are 'smart' references (i.e., ref counted).
::::: -. Java references can be assigned to NULL.
::::: -. Java references can be set multiple times (and have no
::::: restrictions against setting to r-values).
::

Java references are smart pointers. We have got those in C++ as well.



Bo Persson
 
E

Erik Wikström

You are talking about compiler optimizations which can be applied
whether a ptr or ref is used. The same optimizations can be applied
equally well to either (similar to inlining it sounds like) and if you
would accept what I'm telling you, a reference is compiled to exactly
the same thing as a ptr.

Suppose you have a ref like this:

int****&

We would agree on the first 4 asterisks, no? Those are definitely
ptrs, right? It's only that last level of indirection that is in
dispute, right? What construct do we have at our disposal (in
assembly, C, C++) to bridge that last level of indirection? I would
highly recommend that if you decide to write a compiler, that you
consider our old friend-- the ptr!

Of course, but I am not talking about how to implement them. How the
compiler makes a reference do what it does is unimportant, there are
hundreds of ways to implement references (though few effective). What is
important is the fact that pointers and references have very different
semantics.
Imaging that you knew C (or possibly even assembly) before you learned
C++. How did you pass things by reference? I'll leave the code
example for you to write if you don't see this point.

Again, that is an implementation detail and does not have any bearing on
whether references and pointers are similar or not. Would you consider
the following two "concepts" identical just because they have the same
implementation too?

// A point in space
struct Point
{
double x, y, z;
};

// A Point on a curve
struct FuncVal
{
double x; // The x value
double fx; // f(x)
double d; // f'(x)
};
When I say conceptual, I mean literally that--a concept in the brain.
Are you telling me that you can't *think* of references as ptrs under
the skin?

I can think of them that way, but that is not how I think of references,
because that is an implementation detail. When I think of a C++
reference it is in the terms of its semantics.
Have you ever programmed in Java or C#? The reference certainly feels
the same whether you are in C++ or Java or C#. The only difference
are the little gotchas that are more plentiful in C++ (i.e., special
cases) due to what is really a handicapped reference (when compared
objectively to the theoretical concept) that in C++ was no more than a
piece of syntactic sugar for hiding a level of indirection (one level,
presumably the last).

I have programmed in both Java and C# and a few others, but I do not
agree about references feeling the same. For one thing reseating a
reference in both Java and # is natural, in C++ I would have to use a
(smart-)pointer to do that. Nor do I feel that the C++ references are
handicapped versions of the Java and C# references, since they do not
sever the same purpose. Its like saying a drill is a handicapped saw,
sure both make holes, but not the same kinds of holes and you do not use
them for the same tasks.

The Java and C# references are what the C++ pointers would have looked
like if there had been no need for backwards compatibility with C
(pointer arithmetics) and no manual memory management.
For you, the superficial difference is for me the main feature. I see
all the other gotchas regarding refs in C++ as merely superficial side
effects of various constraints operating in conjunction with one
another (i.e., emergence, in this case-- not very pretty emergence).

Not sure what you mean by emergence.
 
E

Erik Wikström

Yes. Exactly. A little digging around the net turned up this page
(which provides a little bkg info on how the 'alias' notion is being
promoted):

http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references

It seems that the idea of an alias is some academic's way of coming up
with a concept that is easier to understand than 'ptr'.

Actually I would think, but I could be wrong, that the idea comes from
TC++PL where it says in §5.5 (freely translated from Swedish) "A
reference is an alternative name for an object." But then again, BS is
an academic.
 
J

johanatan

Not worrying about anything and not worrying about references about the
inner workings of references are two different things. I am positive that
this professor is pushing the students to worry a lot about many things.

You can worry all you want about the inner workings of references, it won't
do you any good because the standard does not specify those. The standard
does not say what references are nor how they work "under the covers".
Thus, you have no means to address you worries. In that sense, the
professor is just stating a truism. The only programmers who have a
meaningful opportunity to worry about how references work "under the
covers" are those programmers that implement C++ compilers.

Not true. We are working with actual compilers. What about those
programmers that need to step code and look at the disassembly to see
where optimizations can occur (not just compiler writers). My point
was that someone who knows how the inner parts work in practice will
be better able to make decisions than those who don't. And, do you
really think, that a person who has been indoctrinated in the 'new C+
+' idea of 'aliases' is never going to encounter an old C-style
programmer on the job. There's no point in hiding this stuff from
them-- if they're any good at all, they're gonna figure it out
completely eventually anyway. If a total novice wants to think of
references as aliases, that's fine, but there's no way you're gonna
convince me that's what they are because I know better. Alias adds
nothing to 'reference'--both are abstract concepts which in practice
turn out to be hidden ptrs.
All other programmers should be busy enough worrying about the life-time of
objects bound to references and the provisions for initializing references..
None of those are intuitive and none of those are successfully captured in
any of the nutshell-explanations for references. I find that neither the
talk about aliases nor the comparison with pointers is helpful in dealing
with references. There is nothing more (but also nothing less!) to
references than the specs from the standard; and those rules are
complicated and cannot be captured by a simple slogan or comparison.

As I said, anytime you start stepping code (especially if you step
into the disassembly) you're going to become familiar with these
things. And, to be honest, for me, it is just simpler to reason about
the reference as what it actually is in practice (and that was in fact
the way I was taught by my professors, who were no doubt well-versed
in C and assembly to begin with). If anyone here can come up with an
alternate implementation of a reference that isn't a pointer, I would
love to see it, but until such a compiler comes out, what harm does it
do to think of them as such? In fact, I actually think of
'references' as 'entities that *refer* to other entities. The whole
'alias' vs 'pointer' debate is merely a difference of nomenclature and
quite superficial.

As I've said before though, I understand that the standard doesn't
require that refs be ptrs, but as anyone coming from assembly or C to C
++, they're gonna immediately recognize them as such and until such
time as the 'theoretical' alternate implementation of a ref that isn't
a ptr comes out, I see no harm in thinking of them as such. And, I
would add, when this 'fantastic' uber-reference comes out, I will have
a compiler and a debugger at that time and will be able to step right
in to it as well and understand exactly what it is (if needed).

Let me ask you this: would you prefer a mechanic that doesn't know
the internals of an electric starter, but can simply determine when
the starter is bad and replace it -or- the one who can break apart the
starter and service the brushes and armature (assuming that its
economical to do so).
If they want students to not worry about
stuff, then they should just become a 'Java school' (though I disagree
with Java programmers not worrying about stuff too).

[snip]

This is converging toward drivel and language wars. It does not add anything
meaningful to the technical discussion of references in C++.

Well, given as how this is a C++ group, I thought you guys might like
the complement (seeing as how some of my other posts seem to have
derided it). But, to be clear, I like to approach each language
objectively and praise those parts which are good and critique those
parts which could use work. I don't know if you read Joel's page or
not, but it is quite insightful (and I figured that the C++ group
would see that point and be able to extrapolate it to the practice of
hiding details of references by calling them aliases). That might
work for ppl who have never seen C or assembly, but what is the real
advantage? If students can't understand ptrs (as Joel said), I think
they're in the wrong major.

I totally take the point that you can't depend on implementation
details, but I see absolutely no reason to indoctrinate people with
yet another imperfect metaphor for something that is quite simple. A
reference is something that 'refers' to something else. That much can
be gathered from the name itself. Alias adds absolutely nothing to
'reference'. If alias was a better word for this, then I think Bjarne
would have chosen it. But, given as how he chose reference, and C++
is an extension of C, it isn't a big leap to think that he was adding
a nice little tight syntax for passing something by reference which C
programmers were very accustomed to already.

Alias means 'another name for something else'. Reference means
'something that refers to something else'. Pointer means 'something
that points to something else', or if you get really technical about
it, a memory slot that contains the address of something else (which
in practice, references are too). To me, if something refers to
something else, it might as well 'point' to it. These are all
ontologically equivalent (i.e., conceptually) and technically in
practice, equivalent (at the assembly code level) at least with the
state of the art compilers.

Since C++ descended from C, you cannot simply look at it in
isolation. You have to look at the parts of C that 'inspired' the
features of C++ (and all of the C features that still remain, as C++
is essentially a superset of C (and yes, if you want to pick at minor
details, the two languages have slightly diverged of late)).

--Jonathan
 
J

johanatan

Actually I would think, but I could be wrong, that the idea comes from
TC++PL where it says in §5.5 (freely translated from Swedish) "A
reference is an alternative name for an object." But then again, BS is
an academic.

Well, for a C programmer, it's going to be very tempting to think of a
reference in the context of 'pass-by-reference' which in C is
accomplished by a ptr. And, that's the main use for references in C++
too, right? To pass something by reference to avoid a potentially
large copy (and to have any changes inside the inner scope propagated
back out to the caller). As I said previously, Bjarne could have
chosen something other than 'reference' and that would have avoided
the almost automatic mental connection to passing by reference in C.

I think if you interviewed some C programmers that made the transition
to C++ in the early days, that you'd find they most likely thought of
them in that way (though I wasn't there and can't say for sure--just
the impression I've gotten from professors and colleagues).

I'm the type of person that is content with abstractions, but after
you know what is *really* going on, why forget it? I can tell you
from experience that none of my programs have ever been adversely
affected by my 'concept' of what a reference is.

--Jonathan
 
J

johanatan

I think he is saying that you shouldn't think of a reference as a
pointer, just because they are implemented similary on some hardware.
Just like you shouldn't think that an int is a long, just becase they
are on some compilers (but not on others).

Point taken. But, if an alternate implementation of references ever
comes out, I'd like to know how it works too, because that's just the
curious person I am. In the grand scheme of things, I think this is
probably something that isn't as likely to change from compiler to
compiler (or in future), but if it ever does, and I need to do some
optimization, then at that time, I will learn the alternate impl.

Can anyone seriously think of an alternate implementation of reference
that didn't include at least a ptr to the original object?

For me, everything that I do in C++ ultimately involves some thought
process about what it must be being compiled to (and in cases where
optimization is called for, an actual reading of the disassembly).
Now, with Haskell, this really is a black-box language. It is not
clear how lazy evaluation and infinite lists and so on translate to
assembly (but in C and C++, it's much easier) so I'm stuck just
working with the abstract concepts in it (at least at this point).

--Jonathan
 
J

johanatan

All other programmers should be busy enough worrying about the life-time of
objects bound to references and the provisions for initializing references..
None of those are intuitive and none of those are successfully captured in
any of the nutshell-explanations for references. I find that neither the
talk about aliases nor the comparison with pointers is helpful in dealing
with references. There is nothing more (but also nothing less!) to
references than the specs from the standard; and those rules are
complicated and cannot be captured by a simple slogan or comparison.

That might be true for you and I do agree that lifetimes and such are
far more important things to worry about. But, I don't see the harm
in thinking of them as ptrs. To each his own I suppose. There seems
to be 3 types of ppl here, those who prefer to think of them as
aliases and those who think of them as wrapped and enhanced (though
limited in other ways) ptrs. And, then you, who sticks directly to
the standard (which I must say I've never read or needed to). I
always let the compiler tell me when I'm trying to do something non-
standard (as it is concerned). And, I've never had to write platform
independent code, so the compiler is usually the last word for me
about standards (and most compilers are non-standard to begin with).

I'm sure everyone involved in this discussion are very competent
programmers and so what works for them, works for them. :)

--Jonathan
 
J

jkherciueh

johanatan said:
Not true. We are working with actual compilers. What about those
programmers that need to step code and look at the disassembly to see
where optimizations can occur (not just compiler writers).

I hope those programmers refer to the documentation of their tool chain, in
particular the debugger. Hopefully, it will tell them how operations with
references are shown.
My point
was that someone who knows how the inner parts work in practice will
be better able to make decisions than those who don't.

Anyone who thinks about references in terms of broken metaphors will make
worse decisions than someone who knows the precise rules.
And, do you
really think, that a person who has been indoctrinated in the 'new C+
+' idea of 'aliases' is never going to encounter an old C-style
programmer on the job. There's no point in hiding this stuff from
them-- if they're any good at all, they're gonna figure it out
completely eventually anyway. If a total novice wants to think of
references as aliases, that's fine, but there's no way you're gonna
convince me that's what they are because I know better.

I never claimed that references are aliases. I specifically stated that the
standard does not say what references are. You seem to think that one has
to think of references either as aliases or as syntactically hidden
pointers. However, neither point of view has any support in the standard.
Alias adds
nothing to 'reference'--both are abstract concepts which in practice
turn out to be hidden ptrs.

I fail to see the significance of this observation. Many abstract concepts
in computer science can be implemented in terms of pointers, e.g.,
functions tend to be represented as addresses of a piece of machine code
used to jump there and one could say that a function call is syntactic
sugar hiding a pointer.

If you insist on adding pictures like that to your mental environment, I
won't try to stop you. I just don't feel the need, and I think that when
push comes to shove those pictures are misleading and harmful.


[snip]


Best

Kai-Uwe Bux
 
J

johanatan

I hope those programmers refer to the documentation of their tool chain, in
particular the debugger. Hopefully, it will tell them how operations with
references are shown.

Yea, sure. That was the point. Even better than documentation would
be the actual disassembly and a debugger that can step into it
(assuming that you already have compiling code).
Anyone who thinks about references in terms of broken metaphors will make
worse decisions than someone who knows the precise rules.

As I said before, precise rules change from compiler to compiler, so I
prefer to go by what restrictions my compiler puts on me.
I never claimed that references are aliases. I specifically stated that the
standard does not say what references are.

I know you didn't. You are in the 'third' category I mentioned in a
previous post--the one that goes by the letter of the standard. My
disagreement was merely with the correction that I should be
*thinking* of references as *aliases*.
You seem to think that one has
to think of references either as aliases or as syntactically hidden
pointers. However, neither point of view has any support in the standard.

Actually, no. I prefer to think of references as references and have
no need for a mental model because I understand both the abstract
concept of something that refers to something else (i.e., a reference)
and the concrete implementation of it in practice (which is by address
and always has been since the days of assembly).

I believe this whole debate was started when I was told that I should
be 'thinking' of references as aliases. But, I have absolutely no
need to do so because for me the word 'reference' and 'alias' are the
same in the abstract (and in the concrete, I understand them as ptrs
with current implementations of compilers, and, I suspect, all
possible efficient implementations).
I fail to see the significance of this observation. Many abstract concepts
in computer science can be implemented in terms of pointers, e.g.,
functions tend to be represented as addresses of a piece of machine code
used to jump there and one could say that a function call is syntactic
sugar hiding a pointer.

Well, the main point of that observation was the first clause--"Alias
adds nothing to 'reference'". These, like many other things in CS or
philosophy are two words for the same concept.

To say that a func call is syntactic sugar is stretching the
conceptualization quite a bit. Of course everything in a program is a
value that exists at an address. But, a reference is (in assembly) a
ptr, nothing more, nothing less. Functions can be represented as
addresses of machine code, yes, and we call those 'function ptrs'. I
suppose one could even theorize about the existence of a 'function
reference', though in C++, we'd probably call something like that a
functor (or first-class function when they arrive on the scene).
If you insist on adding pictures like that to your mental environment, I
won't try to stop you. I just don't feel the need, and I think that when
push comes to shove those pictures are misleading and harmful.

I'm not adding any pictures to my environment. To be honest, I
learned C++ over 10 years ago and do not need the help of mental
models to understand such elementary concepts as references. But, I'm
sure glad that at that time, my professor didn't introduce a new word
that means the same thing--'alias' to *help* me understand it.

It seems from this discussion that C++ is now trying to separate
itself from C (and even further from assembly), but if that is the
real goal, why not go with a more abstract language to begin with--
Java, C#, etc.

What about hard links in an UNIX filesystem? Would you call those
aliases (or even think of them as such)? Maybe you would, but I know
that under the skin it is a 'pointer' to the actual blocks of the
original file. See:

http://en.wikipedia.org/wiki/Hard_link

Maybe you should remove the word 'pointer' there, in the first
sentence, if your new indoctrination is going to be successful (though
it's gonna be even harder to remove it from peoples minds). And, no,
I wasn't the author of that page (and have no idea who was) so I
didn't intentionally put both words in the same sentence as synonyms
to support my argument here.

--Jonathan
 
J

johanatan

If you insist on adding pictures like that to your mental environment, I
won't try to stop you. I just don't feel the need, and I think that when
push comes to shove those pictures are misleading and harmful.

I think we both agree on this point (and most others too actually).
It's just that I think 'alias' is the more offensive mental model than
'pointer' (though surely neither are needed).

Also, please realize that the end of my last msg was in response to
your msg, but was not directed to you (it was to the 'alias'-
supporters).

--Jonathan
 
J

johanatan

Anyone who thinks about references in terms of broken metaphors will make
worse decisions than someone who knows the precise rules.

It would be really nice if people would take my words for what they
say instead of what they want them to say or what they think they
say. The point was that someone who knows the inner workings (as well
as the precise rules) is better able to make decisions than someone
who knows only the precise rules.

This isn't the first time a straw man has been setup and argued
against, but it is a very clear one that I thought I should point out.

--Jonathan
 
J

James Kanze

Consider the following code:
int& r = 5;
If I can bind a reference to a value I can also change that
value. But that is not how a reference works, a reference is a
alias for an object not an object on its own. Therefore a
reference can not bind to a rvalue.

That's an interesting example, and a very good reason for not
allowing using an rvalue to initialize a reference. Still, the
original language (pre-1988, roughly, so we're talking about a
very long time ago) did allow using an rvalue to initialize any
reference, with more or less the same semantics which currently
apply when an rvalue is used to initialize a reference to const.
The rule was changed because it was found, in practice, to be
too great a source of errors. IMHO, the real problem is that
there are too many implicit conversions, and that the result of
an implicit conversion is an rvalue; the errors are typically
cases where an implicit conversion resulted in an rvalue when
the programmer expected an lvalue. Things like:

void
increment( int& i )
{
++ i ;
}

int
main()
{
unsigned x = 0 ;
increment( x ) ;
std::cout << x << std::endl ;
return 0 ;
}

which, of course, results in 0.
 
J

James Kanze

That just isn't true (on both technical and conceptual levels).
Technically, a reference in C++ is just a ptr with syntactic sugar to
allow using it just like the object itself instead of dereferencing
the ptr (or using the -> operator).

Not really. A C++ reference is a more or less an alias for an
object. It can't be null, and it can't be reseated.
Comparisons with Java and C# don't make much sense, either,
because the object model is different: in C++, a pointer is an
object, just like an int or anything else, and you can take the
actual address of anything---in Java (and C#?), only class types
can be objects, you can only have a reference (or pointer) to a
class type, and all objects of class types must be dynamically
allocated. Globally considered, references in Java are as exact
an equivalent to C++ pointers as you can have, given the object
model of Java.
Conceptually, they are the same in the two languages (barring
the restrictions already mentioned that C ++ enforces). The
only differences I'm aware of between C#/Java and C+ + with
regards to references are:
-. Java references are 'smart' references (i.e., ref counted).

Not in any Java implementation I've seen or heard of.
-. Java references can be assigned to NULL.

Like C++ pointers.
-. Java references can be set multiple times (and have no
restrictions against setting to r-values).

Java doesn't have rvalues, at least, not in the sense C++ does.
In C++, a pointer can be set multiple times, as in Java. And
you need to jump through hoops to get it to point to an rvalue,
since you cannot directly take the address of an rvalue (and of
course, an object allocated by new is not an rvalue).

So far, you've shown that references in Java are the equivalent
of pointers in C++, not the equivalent of references.
Under the skin, they are both ptrs

That's one possible implementation of references in C++, but the
language has been carefully designed so that a conforming
program cannot tell.
(while the Java variant has an additional ref count and
possibly other bookeeping-related (i.e., garbage collector)
metadata).
I suspect that the reference idea was a step forward for C++
from C (adding only the syntactic sugar over a regular ptr)
and that the designers of Java took the idea one step further.

No. I think that the original motivation for references in C++
is linked with operator overloading; they allow writing an
operator+ which doesn't require a deep copy of both of its
arguments, but can still be called with the usual syntax. The
designers of Java based there references on C++ (and C)
pointers, simply eliminating features which they felt were too
dangerous (like pointer arithmetic) or which didn't fit into the
object model. (For that matter, pointer arithmetic really
wouldn't fit into the Java object model either.) The choice of
the name was purely based on the supposed reputation that
pointers were dangerous, and that in a "safe" language, you
can't have pointers. (Marketing, in sum.)
 
J

jkherciueh

johanatan said:
It would be really nice if people would take my words for what they
say instead of what they want them to say or what they think they
say. The point was that someone who knows the inner workings (as well
as the precise rules) is better able to make decisions than someone
who knows only the precise rules.

I do not engage in reading minds but only in reading what people write. Your
claim did read like so:

someone who knows how the inner parts work in practice will
be better able to make decisions than those who don't

and neither like this:

someone who knows how the inner parts work and knows the precise rules
will be better able to make decisions than those who don't.


nor like this:

someone who knows how the inner parts work and knows the precise rules
will be better able to make decisions than those who only know the rules.

Besides, if you read what you quote carefully, you will find that I did not
contradict you anyway. I just provided an additional point of view.


With regard to your clarified position, I find that the situations where
knowledge of the inner workings of references (given a particular compiler
and in addition to knowledge of the rules from the standard) is useful in
decision making are rather limited (in fact, just about nothing comes to
mind). However, that might just be a lack of imagination and experience on
my part. Could you provide some examples for the kind of decision making
you are thinking of?


Best

Kai-Uwe Bux
 

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

No members online now.

Forum statistics

Threads
473,985
Messages
2,570,199
Members
46,766
Latest member
rignpype

Latest Threads

Top