This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: vector<>.begin()
- From: Carlo Wood <carlo at alinoe dot com>
- To: Jeff Williams <jwilliams at mfchelp dot com>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Fri, 7 Nov 2003 16:40:56 +0100
- Subject: Re: vector<>.begin()
- References: <20031107141753.MQQV1539.fep01.biz.rr.com@[127.0.0.1]>
On Fri, Nov 07, 2003 at 09:17:53AM -0500, Jeff Williams wrote:
> operator pointer() const { return _M_current; }
>
> This fixes the problem of vector.begin() not being a ptr to the first element and is syntatically cleaner that &*v.begin();
If you don't like that syntax, then you could use:
v.begin().base();
or add member to the class it contains called 'ptr()'
that returns 'this' and use:
v.begin()->ptr();
At least that would be portable :).
--
Carlo Wood <carlo@alinoe.com>
#include <vector>
#include <cassert>
struct A {
A* ptr(void) { return this; }
A const* ptr(void) const { return this; }
};
int main(void)
{
std::vector<int> v;
v.push_back(123);
int* p = v.begin().base();
assert( p == &(*v.begin()) );
assert( *p == 123 );
std::vector<A> w;
w.push_back(*new A);
A* a = w.begin()->ptr();
assert( a == &(*w.begin()) );
return 0;
}