Topic: uncomplete reference implementation in C++
Author: hendrik@vedge.com (Hendrik Boom)
Date: Tue, 17 Aug 1993 15:17:58 GMT Raw View
PostMaster@barclays.co.uk writes:
: In article <1993Aug09.183812.23731@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
:
:
: > | intref = 1; // assignment to 'a'
: > |
: > | intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
: > | // not valid C++ code!
: >
: > No. This would be breaking existing 'sensible' type rules. You'd
: > need to introduce new syntax to represent 'reinit' of a
: > reference. I'd suggest:
: >
: > intref := b;
: >
: > | intref = a; // assigns the value of 'a' to 'b'
: > | }
: > |
: > |Anybody else who'd like to have such an extension to C++? Comments? Facts?
:
: Coo, Algol-68
The thing that makes C++ toerable as a language is that, if you are careful,
you can make it behave a bit like Algol 68.
:
: --
: Colin Walls | Nothing is so good that somebody,
: Colin.Walls@barclays.co.uk | somewhere, will not hate it.
: Tel: 0565-621000 x 4531 |
: | Pohl's law
--
-------------------------------------------------------
Try one or more of the following addresses to reply.
at work: hendrik@vedge.com, iros1!vedge!hendrik
at home: uunet!ozrout!topoi!hendrik
Author: jimad@microsoft.com (Jim Adcock)
Date: 09 Aug 93 18:38:12 GMT Raw View
In article <23t4sd$1g3@scax18.pki-nbg.philips.de> ln_mhr@hitkw18.pki-nbg.philips.de (M. Hrnjad) writes:
>
|Hello netters,
|Compared to pointers their implementation is very uncomplete. What I'd
|like to see in C++ is something like:
|
| void main(void)
| {
| int a, b;
| int &intref = a; // already possible with current C++, 'intref' refers
| // to 'a' now
In C++ today references are implicitly 'const'. This would have to be
changed.
| intref = 1; // assignment to 'a'
|
| intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
| // not valid C++ code!
No. This would be breaking existing 'sensible' type rules. You'd
need to introduce new syntax to represent 'reinit' of a
reference. I'd suggest:
intref := b;
| intref = a; // assigns the value of 'a' to 'b'
| }
|
|Anybody else who'd like to have such an extension to C++? Comments? Facts?
I don't think you'd get much support for these ideas. Seems that most
committee members still unfortunately prefer pointers to references,
so they are not motivated to 'fix' nor extend reference capabilities.
Note that in some sense C++ does not introduce references, C++ only
introduces the notion of explicitly declared references used as aliases
for already named objects:
int foo;
always implicitly declared a reference named 'foo' referring to an int object.
In this sense current C++ behavior is consistent in not allowing
you to 'reassign' references.
int foo;
int& bar = foo;
int foo2;
foo := foo2; // reinit of implicit references not allowed
bar := foo2; // reinit of explicit references not allowed neither
IMHO pointer and reference definitions in C/C++ are so broken as to
hardly be fixable. One tiny first step in the right direction would
be to have clear rules about what kinds of pointer/object compatibility
are allowed, and what aliases are allowed. [ANSI C is more clear than
C++ in this regard] Another step in the right direction would be to kill
dead the historical equivalencies between pointers and ints, once and for
all.
Author: PostMaster@barclays.co.uk
Date: 11 Aug 93 15:39:58 GMT Raw View
In article <1993Aug09.183812.23731@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
> | intref = 1; // assignment to 'a'
> |
> | intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
> | // not valid C++ code!
>
> No. This would be breaking existing 'sensible' type rules. You'd
> need to introduce new syntax to represent 'reinit' of a
> reference. I'd suggest:
>
> intref := b;
>
> | intref = a; // assigns the value of 'a' to 'b'
> | }
> |
> |Anybody else who'd like to have such an extension to C++? Comments? Facts?
Coo, Algol-68
--
Colin Walls | Nothing is so good that somebody,
Colin.Walls@barclays.co.uk | somewhere, will not hate it.
Tel: 0565-621000 x 4531 |
| Pohl's law
Author: ln_mhr@hitkw18.pki-nbg.philips.de (M. Hrnjad)
Date: 6 Aug 1993 08:31:09 GMT Raw View
Hello netters,
it has always annoyed me that references have been introduced to C++
only so far to provide the call-by-reference for functions.
Compared to pointers their implementation is very uncomplete. What I'd
like to see in C++ is something like:
void main(void)
{
int a, b;
int &intref = a; // already possible with current C++, 'intref' refers
// to 'a' now
intref = 1; // assignment to 'a'
intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
// not valid C++ code!
intref = a; // assigns the value of 'a' to 'b'
}
Anybody else who'd like to have such an extension to C++? Comments? Facts?
M. Hrnjad
--
Muharem Hrnjadovic (ln_mhr@pki-nbg.philips.de)
Philips Kommunikations Industrie +49 911 526 6511
Author: b91926@fnclub.fnal.gov (David Sachs)
Date: 6 Aug 1993 20:16:54 GMT Raw View
In article <23t4sd$1g3@scax18.pki-nbg.philips.de>, ln_mhr@hitkw18.pki-nbg.philips.de (M. Hrnjad) writes:
|>
|> Hello netters,
|>
|> it has always annoyed me that references have been introduced to C++
|> only so far to provide the call-by-reference for functions.
|>
|> Compared to pointers their implementation is very uncomplete. What I'd
|> like to see in C++ is something like:
|>
|> void main(void)
|> {
|> int a, b;
|> int &intref = a; // already possible with current C++, 'intref' refers
|> // to 'a' now
|>
|> intref = 1; // assignment to 'a'
|>
|> intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
|> // not valid C++ code!
|>
|> intref = a; // assigns the value of 'a' to 'b'
|> }
|>
|> Anybody else who'd like to have such an extension to C++? Comments? Facts?
Unfortunately, your example IS valid c++ code (except for a missing semicolon), with a very different meaning from what you intend.
...
intref = &b; // convert address of b to an integer ands assign to a
...
You probably could modify the values of references, by using (non-portable) tricks to change the underlying memory.
Actually, you can define a class object that will act as if it were a smart reference. Something like: (please excuse omissions and errors)
class int_reference
{
private:
int *ip;
public:
int_reference(int &i) {ip=&i;}
operator int() {return *ip;}
int_reference& operator=(int i) {*ip=i;return this;}
int_reference& operator=(int *i) {ip=i;return this;}
int * operator&() {return ip;}
}
Author: heliotis.roch803@xerox.com (Jim Heliotis)
Date: 6 Aug 93 18:35:27 GMT Raw View
In article 1g3@scax18.pki-nbg.philips.de, ln_mhr@hitkw18.pki-nbg.philips.de (M. Hrnjad) writes:
>it has always annoyed me that references have been introduced to C++
>only so far to provide the call-by-reference for functions.
>
>Compared to pointers their implementation is very uncomplete. What I'd
>like to see in C++ is something like:
>
> void main(void)
> {
> int a, b;
> int &intref = a; // already possible with current C++, 'intref' refers
> // to 'a' now
>
> intref = 1; // assignment to 'a'
>
> intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
> // not valid C++ code!
>
> intref = a; // assigns the value of 'a' to 'b'
> }
>
It has always been my understanding that references are (1) a notational
convenience, but also (2) pointers made safer through restrictions.
Author: lenngray@netcom.com (Lenny Gray)
Date: Sat, 7 Aug 1993 07:01:03 GMT Raw View
Jim Heliotis (heliotis.roch803@xerox.com) wrote:
: RE:
: >it has always annoyed me that references have been introduced to C++
: >only so far to provide the call-by-reference for functions.
: >
: >Compared to pointers their implementation is very uncomplete. What I'd
: >like to see in C++ is something like:
: >
: > void main(void)
: > {
: > int a, b;
: > int &intref = a; // already possible with current C++, 'intref' refers
: > // to 'a' now
: > intref = 1; // assignment to 'a'
: > intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
: > intref = a; // assigns the value of 'a' to 'b'
: > }
: >
: It has always been my understanding that references are (1) a notational
: convenience, but also (2) pointers made safer through restrictions.
: From your example, your not being able to reassign a ref means that there
: is no doubt to what the "ref is reffing" at any point in the program,
: thereby improving validatability or whatever.
: In other words, if you want the above ability, then use pointers; they
: do everything refs can do, and more;
I agree completely. Generality is nice for mathematicians, but in real
life, practicality is _much_ more important. If a few asterisks in one's
code is too much of an annoyance, how can this guy stand the braces?
And for that matter, the semicolons!
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sun, 8 Aug 1993 13:59:58 GMT Raw View
In article <23t4sd$1g3@scax18.pki-nbg.philips.de> ln_mhr@hitkw18.pki-nbg.philips.de (M. Hrnjad) writes:
>
>Hello netters,
>
>it has always annoyed me that references have been introduced to C++
>only so far to provide the call-by-reference for functions.
>
>Compared to pointers their implementation is very uncomplete. What I'd
>like to see in C++ is something like:
>
> void main(void)
> {
> int a, b;
> int &intref = a; // already possible with current C++, 'intref' refers
> // to 'a' now
>
> intref = 1; // assignment to 'a'
>
> intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
> // not valid C++ code!
> intref = a; // assigns the value of 'a' to 'b'
> }
>
>Anybody else who'd like to have such an extension to C++? Comments? Facts?
Is there something wrong with pointers?
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd, CSERVE:10236.1703
6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
NSW 2131, AUSTRALIA
Author: dak@messua.informatik.rwth-aachen.de (David Kastrup)
Date: 9 Aug 1993 16:02:44 GMT Raw View
maxtal@physics.su.OZ.AU (John Max Skaller) writes:
>In article <23t4sd$1g3@scax18.pki-nbg.philips.de> ln_mhr@hitkw18.pki-nbg.philips.de (M. Hrnjad) writes:
>>
>>Hello netters,
>>
>>it has always annoyed me that references have been introduced to C++
>>only so far to provide the call-by-reference for functions.
>>
>>Compared to pointers their implementation is very uncomplete. What I'd
>>like to see in C++ is something like:
>>
>> void main(void)
>> {
>> int a, b;
>> int &intref = a; // already possible with current C++, 'intref' refers
>> // to 'a' now
>>
>> intref = 1; // assignment to 'a'
>>
>> intref = &b // assignment to 'intref'. 'intref' refers to 'b' now
>> // not valid C++ code!
>> intref = a; // assigns the value of 'a' to 'b'
>> }
>>
>>Anybody else who'd like to have such an extension to C++? Comments? Facts?
This is a pretty bad suggestion, because the expression intref would
have to mean different things depending on the right side of the assignment,
which does not fit in the current type paradigma.
However, instead of writing
intref = &b;
In order to reassign intref, you could use
&intref = &b;
See the trick? &intref might become an lvalue. This would, however,
make
int & const a; // unmodifiable behaviour
(with the old behaviour) different from
int & a; // modifiable behaviour
It would, in addition, imply that there is really no difference between
pointer types and references, except for syntactic differences.
It would be a major problem to ensure that temporaries bound to a
reference are properly deleted when the reference gets reassigned
(they now do on desctruction of the reference).
> Is there something wrong with pointers?
>--
> JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
> Maxtal Pty Ltd, CSERVE:10236.1703
> 6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
> NSW 2131, AUSTRALIA
--
David Kastrup dak@pool.informatik.rwth-aachen.de
Tel: +49-241-72419 Fax: +49-241-79502
Goethestr. 20, D-52064 Aachen