 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
christopher diggins Guest
|
Posted: Mon Apr 11, 2005 5:57 pm Post subject: separation of pointer types |
|
|
What is currently referred to as a pointer in C++ is in my opinion several
different types inappropriately amalgamated as one. Consider how there are
four very clear categories of pointers with entirely different interfaces:
1) the result of an address-of operation
- supports indirection
- supports comparison
- increment/decrement undefined
- delete undefined
- delete[] undefined
2) reference to an element of an array
- supports indirection
- supports comparison
- supports increment/decrement
- delete undefined
- delete[] undefined
3) reference to a dynamically allocated array
- supports indirection
- supports comparison
- supports delete[]
- delete undefined
4) reference to a dynamically allocated object
- supports indirection
- supports comparison
- delete[] undefined
- supports delete
I see no reason these four cases should not each have a separate type. Does
anyone else find the the hypothetical idea of separation of pointers into
different types to have some allure?
--
Christopher Diggins
Object Oriented Template Library (OOTL)
http://www.ootl.org
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Tue Apr 12, 2005 9:49 am Post subject: Re: separation of pointer types |
|
|
[email]cdiggins (AT) videotron (DOT) ca[/email] ("christopher diggins") writes:
| Quote: | What is currently referred to as a pointer in C++ is in my opinion
several different types inappropriately amalgamated as one. Consider
how there are four very clear categories of pointers with entirely
different interfaces:
1) the result of an address-of operation
- supports indirection
- supports comparison
- increment/decrement undefined
- delete undefined
- delete[] undefined
2) reference to an element of an array
- supports indirection
- supports comparison
- supports increment/decrement
- delete undefined
- delete[] undefined
3) reference to a dynamically allocated array
- supports indirection
- supports comparison
- supports delete[]
- delete undefined
4) reference to a dynamically allocated object
- supports indirection
- supports comparison
- delete[] undefined
- supports delete
I see no reason these four cases should not each have a separate
type.
|
Backward compatibility is a very strong reason.
| Quote: | Does anyone else find the the hypothetical idea of separation of
pointers into different types to have some allure?
|
Yes. Which is why we have a plethora of smart pointer and container
types that take care of cases 3) and 4).
The difference between 1) and 2) is blurry, by the way:
void f(int &i)
{
some_pointer_type pi(&i);
}
int main()
{
int i(0);
f(i);
int is[2] = { 0 };
f(is[1]);
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
John Nagle Guest
|
Posted: Tue Apr 12, 2005 9:50 am Post subject: Re: separation of pointer types |
|
|
christopher diggins wrote:
| Quote: | What is currently referred to as a pointer in C++ is in my opinion several
different types inappropriately amalgamated as one.
|
I tend to agree, but it's one of those things that's too late
to fix in this language.
C++ has the needed concepts: arrays,
iterators, and references. But they're all convertable
to and from pointers, and most programmers don't see them
as distinct.
The array-as-pointer thing and the concept of pointer
arithmetic are there mostly because they mapped well to
PDP-11 hardware. Nobody has put that in a new language
in a long time.
John Nagle
Animats
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
kuyper@wizard.net Guest
|
Posted: Wed Apr 13, 2005 6:52 am Post subject: Re: separation of pointer types |
|
|
christopher diggins wrote:
| Quote: | What is currently referred to as a pointer in C++ is in my opinion
several
different types inappropriately amalgamated as one. Consider how
there are
four very clear categories of pointers with entirely different
interfaces:
1) the result of an address-of operation
- supports indirection
- supports comparison
- increment/decrement undefined
|
Incorrect; increment is allowed, decrement isn't.
| Quote: | - delete undefined
- delete[] undefined
2) reference to an element of an array
- supports indirection
- supports comparison
- supports increment/decrement
|
Decrement is not allowed for a pointer to the first element of an
array.
| Quote: | - delete undefined
- delete[] undefined
3) reference to a dynamically allocated array
- supports indirection
- supports comparison
|
You left out:
- supports increment
| Quote: | - supports delete[]
- delete undefined
4) reference to a dynamically allocated object
- supports indirection
- supports comparison
|
You left out:
- supports increment
| Quote: | - delete[] undefined
- supports delete
|
Notice: the (corrected) differences with regard to increment and
decrement are all covered by one principle: a pointer to a single
object is treated as a pointer to the only element of a 1-element
array. It's never legal to decrement a pointer to the first element of
an array. It's always legal to increment a pointer to an element of an
array.
| Quote: | I see no reason these four cases should not each have a separate
type. Does
anyone else find the the hypothetical idea of separation of pointers
into
different types to have some allure?
|
You'll have to identify the advantages. I don't see any.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Dave Thompson Guest
|
Posted: Mon Apr 18, 2005 5:49 pm Post subject: Re: separation of pointer types |
|
|
On Tue, 12 Apr 2005 09:50:32 GMT, [email]nagle (AT) animats (DOT) com[/email] (John Nagle)
wrote:
<snip>
| Quote: | The array-as-pointer thing and the concept of pointer
arithmetic are there mostly because they mapped well to
PDP-11 hardware. Nobody has put that in a new language
in a long time.
Actually they mapped better onto the word machines where BCPL was |
first implemented, like PDP-10 and I think 709x. AIUI it was actually
the PDP-11's need for differing strides that drove _reintroduction_ of
types into C over B, but without dropping pointer arithmetic which has
since been such an opportunity for, ahem, infelicities.
Have there been any new SILs, but Oberon by Wirth who always goes for
safe? The entire category seems to have fallen out of favor.
- David.Thompson1 at worldnet.att.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|