vector<>.begin()

Carlo Wood carlo@alinoe.com
Fri Nov 7 15:40:00 GMT 2003


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;
}



More information about the Libstdc++ mailing list