GNU g++  v5.2.1
GNU Standard C++
rc_string_base.h File Reference

This is an internal header file, included by other library headers. More...

Include dependency graph for rc_string_base.h:
This graph shows which files directly or indirectly include this file:

Functions

namespace __gnu_cxx _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This is an internal header file, included by other library headers.

Do not attempt to use it directly. {ext/vstring.h}

Function Documentation

namespace __gnu_cxx _GLIBCXX_VISIBILITY ( default  )

Documentation? What's that? Nathan Myers ncm@c.nosp@m.antr.nosp@m.ip.or.nosp@m.g.

A string looks like this:

1  [_Rep]
2  _M_length
3 [__rc_string_base<char_type>] _M_capacity
4 _M_dataplus _M_refcount
5 _M_p ----------------> unnamed array of char_type

Where the _M_p points to the first character in the string, and you cast it to a pointer-to-_Rep and subtract 1 to get a pointer to the header.

This approach has the enormous advantage that a string object requires only one allocation. All the ugliness is confined within a single pair of inline functions, which each compile to a single add instruction: _Rep::_M_refdata(), and __rc_string_base::_M_rep(); and the allocation function which gets a block of raw bytes and with room enough and constructs a _Rep object at the front.

The reason you want _M_data pointing to the character array and not the _Rep is so that the debugger can see the string contents. (Probably we should add a non-inline member to get the _Rep for the debugger to use, so users can check the actual string length.)

Note that the _Rep object is a POD so that you can have a static empty string _Rep object already constructed before static constructors have run. The reference-count encoding is chosen so that a 0 indicates one reference, so you never try to destroy the empty-string _Rep object.

All but the last paragraph is considered pretty conventional for a C++ string implementation.

References __catch, __gnu_profile::__size(), __throw_exception_again, and __try.

Here is the call graph for this function: