On Tuesday, May 3, 2016 at 11:02:33 PM UTC-4, Jerry Stuckle wrote:
> On 5/3/2016 10:21 PM, Rick C. Hodgin wrote:
> > On Tuesday, May 3, 2016 at 9:10:00 PM UTC-4, Jerry Stuckle wrote:
> >> On 5/3/2016 1:26 PM, Rick C. Hodgin wrote:
> >>> On Tuesday, May 3, 2016 at 1:04:21 PM UTC-4, Jerry Stuckle wrote:
> >>>> They *could* have done a lot of things. But your method violates the
> >>>> concept of a reference - not having to use special syntax in the code to
> >>>> pass or use the reference.
> >>>
> >>> I WANT to use special syntax to pass by reference. I want it to show up
> >>> in source code so I know it's a reference rather than a value pass.
> >>
> >> That's not how references work, Rick. The whole purpose is to *make them
> >> transparent*.
> >
> > Your posts are often difficult for me, Jerry, because you seem to only be
> > able to relate to the way things are, rather than to step outside of the
> > box a little and understand why someone would mention something like this.
> >
>
> I relate to the way things are because THAT'S THE WAY THINGS ARE. If
> you don't like it - either suggest a change or use another language.
The bulk of my reason for posting today was to propose alternatives, which
I have done. And as I've indicated in this thread, I have witnessed the
difficulty in getting anything new added to the C or C++ standard. I
don't expect anything to change, but my goals here aren't particularly
for C++ to change, but to learn of the fundamental reasons why that which
is in existence is, in fact, in existence.
My goals are CAlive... and I am focused on addressing this issue there.
> In
> this case, the change would not be accepted because it would break most
> of the C++ programs in existence.
It would break no C++ programs in existence because the feature would be
a new feature that would have to be enabled to be enforced. All legacy
code would work without the new syntax constraint, unless that constraint
were enabled in the compiler. Only then would it potentially break old
code. But, like renaming a global variable temporarily to find all of
its usages in a program by looking at the compiler errors, the source
code that was broken by the new syntax requirement would be easily
found, and resolved.
> > I mention it because this...:
> >
> > int x, y, z;
> > // x,y,z are populated
> > some_function(x, y, z);
> >
> > ...doesn't convey enough about the operation to know how x, y, and z are
> > used. They could be updated, or not. It's a lacking feature in C++, and
> > while I recognize the way the syntax works today, I think that decision
> > was wrong, and that it should possibly have an option to allow it to not
> > have something like the @x syntax, but I think by default the visual cue
> > should be included and available.
> >
>
> If they aren't going to be updated, then use const, i.e.
> void some_function(const int x, int y, const int z);
>
> In this case, x and z would not be updated, but y may be.
And we're again back to square one, that you don't know in the source
code whether or not things are being passed are by value, or by reference,
without the syntax markup.
By requiring the @x syntax for by reference values, and the x' for non-
const values, and x! for const values, it would resolve that issue. I'm
still not convinced of the x' and x! syntax, though they are growing on
me.
> >> If you want to use "special syntax", use pointers.
> >
> > I want to use special syntax on the passing of parameters, but not on
> > every subsequent use (dereferencing, using -> instead of . and so on).
>
> You have a choice - use it all the time, not none of the time. You
> can't pick and choose when you might want to use it.
The only place they need to be used are when being passed to something
that can change their value. When they're used within the function,
they are simply whatever type they are, even if they're by-reference
types to some remote data.
> > Pointers also convey baggage that values passed by reference don't,
> > such as the need to check for NULL values, which slows down processing,
> > and is redundant when using references.
>
> Actually, not true. It is possible to pass a reference to NULL -
> although you have to work at it.
I know of know way apart from doing something underhanded, such as using
a union with overloaded function calls, or calling a function with a
different function declaration which does not match the definition.
Internally, the source code generated for both of these is identical.
The pointers to main's x,y,z are pushed onto the stack, and their
values are updated through the pointer using identical assembly code:
void populate1(int& x, int& y, int& z)
{
x = 12; y = 13; z = 14;
}
void populate2(int* x, int* y, int* z)
{
*x = 22; *y = 23; *z = 24;
}
int main(int argc, char* argv[])
{
int x,y,z;
populate1(x,y,z);
populate2(&x, &y, &z);
return(0);
}
The compiler hides the relaxed syntax requirements on by-ref values for
you, but they are still internally identical to pointers.
This is why they're so desirable. It is greatly simplified syntax in
many cases, and it removes the NULL check requirement. The only issue
that is an issue is that you don't know in source code if things are
being passed by value or reference by mere inspection. It's my only
point in this thread.
> > There are reasons to use by-reference values. And for those reasons I
> > want to use them. I just want my source code to indicate to me when I
> > use them that I'm actually using them.
>
> You can use them all of the time, or none of the time. But the function
> itself can't accept a reference sometimes and a copy other times - just
> like it can't accept a pointer sometimes, and a copy other times.
FWIW, nobody's asking it to accept something one way part of the time, or
not another time. It will always be what it is. My point is that whenever
something is passed by reference, it should include the additional syntax.
It should not be required in other places.
> Use references all of the time, or copies all of the time when calling a
> function. You can't do both.
You've misunderstood somewhere, Jerry.