I am not messing with semantics |
Yes, you are.
The OP isn't interested in being a compiler. The OP is interested in getting some type of OBJECT into his function. When you start talking about an intermediate object (a pointer) as if it were the OBJECT (the integer, or whatever is being addressed), then things are changed.
We are looking at the same thing in two different ways, and that is why we are at odds here. Stroustrup is interested in thinking like a compiler. People who write compilers think like compilers. But a compiler is the implementation of a language specification, which is not a compiler (though many language documents, C and C++ documents in particular, treat it that way).
I would say that you can pass/return a value, a reference, or a pointer. |
Er, isn't that the same as saying "pass by value", "pass by reference", and "pass by pointer"? This is the semantic I am talking about.
You say, "No it is not the same" because you are interested in the mechanics of how the passing happens.
I say that the
way the target OBJECT is accessed is what is at issue here.
- by value - a copy of OBJECT is given to access
- by pointer - a pointer referencing the OBJECT is given
- by reference - an alias to the OBJECT is given
If you stick with the "a pointer is a value" way of wording things, then all the existing C literature wherein we pass by reference must be invalidated... since C doesn't actually have a "reference" type, and all arguments are actually passed by value on the call frame. A pointer
is a reference, just as it was in C. It is an
indirect reference, just as it was in C, but it is a reference none-the-less.
How do we distinguish between an indirect reference via pointer and a direct reference via alias when talking about it? Terminology, of course.
An indirect reference involves an intermediate object - a "pointer".
A direct reference does not (or at least it hides it if it does) - a "reference".
This is the semantics, or
meaning, given when we ask how an object is passed to a function. We don't care about the meaning of how the intermediate objects, if any, are passed.
Duoas wrote:
a copy of the address of the integer is made |
A copy of a pointer is made. A pointer is not the same thing as an address. A pointer is a complete object that can hold an address value; an address is just a concept, a number. |
NO. A copy of the
address is made.
A
pointer is a
type.
An
address is a
value.
Types: int, bool, double, std::string, ...
Values: 47, true, -3.219, "Hello world", ...
You don't pass
types, you pass
values.
Duoas wrote:
In all cases, we are interested in passing an int value to the function. |
Just not true. Maybe you never have the need to have functions work on pointers (rather than the object obtained by dereferencing a pointer), but I do and so do lots of other people. |
You are committing a red herring and attempting others...
If the OBJECT being given as argument to the function is a pointer type, then there are three ways to pass that pointer:
1 2 3
|
void foo( int* ) -- by value
void foo( int** ) -- by pointer
void foo( int*& ) -- by reference
|
And, of course, inside the function you will dink with it the same ways:
1 2 3
|
int x = 42;
int* n = &x;
foo( n, &n, n );
|
1 2 3 4 5 6 7
|
int y = -7;
void foo( int* pv, int** pp, int*& pr )
{
pv = &y; // n is not modified
*pp = &y; // n now addresses y (instead of x)
pr = &y; // n now addresses y (instead of x)
}
|
This is more easily observed if you use a friendly
typedef:
1 2 3
|
void foo( pint ) -- by value
void foo( pint* ) -- by pointer
void foo( pint& ) -- by reference
|
1 2 3
|
int x = 42;
pint n = &x;
foo( n, &n, n );
|
1 2 3 4 5 6 7
|
int y = -7;
void foo( pint pv, pint* pp, pint& pr )
{
pv = &y; // n is not modified
*pp = &y; // n now addresses y (instead of x)
pr = &y; // n now addresses y (instead of x)
}
|
@
Grey Wolf
I also have a pet peeve when terms that have specific meanings are applied in the wrong context.
I believe you have chosen a context convenient to writing compilers.
I also believe that the OP is not interested in writing a compiler, but in understanding the functioning of the language implemented by compilers.
Our contexts differ. And all meaning has value only in context.
Also, I have not made any term used here meaningless or given it any kind of negative connotation. Please watch your barbs, as I have not treated you unkindly. I only said that I think you are mixing contextual meanings, ie semantics -- and I made no judgement on whether or not that was intentional. (But I'll say here that I don't think it was. I just think that you are looking at this from a different angle, ie context, and I think that your angle is not the OP's interest.)