This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: vector<>.begin()


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]