vector::operator[]

Howard Hinnant hhinnant@apple.com
Tue Nov 29 17:07:00 GMT 2005


On Nov 29, 2005, at 5:26 AM, Paolo Carlini wrote:

> By the way, to the attention of Howard, in particular, I would be
> interested in learning more about Metrowerks data() + n instead of
> begin() + n implementation of vector<T>::operator[]... I don't see why
> we couldn't (shouldn't) do that in libstdc++ too and avoid a good  
> amount
> of ADL troubles...

There's not too much to learn here really.  This was a happy accident  
that was made more likely due to an overall coding style I have  
(which tends to make happy accidents more likely to occur).

That philosophy is basically:  Keep things as simple as possible.   
Don't gratuitously add code just to see if the compiler can optimize  
it back out.

Doing so may not make any difference in release code.  But it can  
significantly speed up debug mode if code does not gratuitously step  
through several layers of inline functions.  And there is nothing  
wrong with speeding up debugging code when there is no cost in doing so.

In this particular example, I knew that vector::iterator was a  
wrapper class.  The wrapper class is a very thin layer that should be  
optimized away in release mode, but definitely is not optimized away  
in debug mode.  Internal to vector one has a pointer to the data.   
There is very little to be gained (actually nothing) by implementing  
vector's internals with iterators instead of pointers.  I.e. Keep  
things as simple as possible.

-Howard



More information about the Libstdc++ mailing list