* John Salmon:
>
> So, can I write:
>
> T *p = &(v[0]);
>
> or do I have to write:
>
> T *p = (v.size()!=0)? &(v[0]) : 0;
The latter.
§23.1/7 "If the container is empty, then 'begin() == end()'".
§24.1/5 Explaining that one-past-the-end iterators are past-the-end
values, then "The library never assumes that past-the-end values are
dereferencable".
Hence v[0] by equivalence to &(*(begin()+0)) performs an invalid
dereferencing when the vector is empty. In more practical terms, the
vector need not have a buffer if it's empty. But I suspect that with
most compiler and standard library implementations (all?) you can get
away with it if the vector's /capacity/ is greater than zero.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
---
[ 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.comeaucomputing.com/csc/faq.html ]