Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'base' no member of iterator in C++ anymore

I am trying to compile some code that was originally build in MS Visual Studio C++ 6.0 with Visual Studio 2013.

In the old code the following construct was often used for various data types (here for example for a string):

std::string someString;
someString = ....;
callSomeFunction(someString.begin().base());

So in order to enable the function to edit (in this example the string) the original buffer, the method begin() gets an iterator for it and the method base() returned a pointer to the first element of the buffer.

In the code this is not only used for strings, but for many many other datatypes as well. Thus I was wondering, if there was a change to the standard library so the base() method is no longer supported?

Is there any replacement for it? Or do I have to change the code here? Since this was used very frequently, I would prefer to find a simpler solution to this.

Currently I get an error like:

Error 3 error C2039: 'base' : is not a member of 'std::_Vector_iterator>>'

like image 656
Merold Avatar asked Mar 17 '23 10:03

Merold


1 Answers

base was not standardized for container iterators. It exists for iterator adaptors such as std::reverse_iterator and std::move_iterator. Microsoft had to remove it from their implementation in order to comply with the ISO C++ standard.

To obtain a pointer from a container iterator, simply use &* iter. To get a pointer to the first element of the contiguous array backing a container, use cont.data().

like image 110
Potatoswatter Avatar answered Mar 24 '23 23:03

Potatoswatter