There are proposals to add string_view
to C++ in an upcoming standard.
A string_view
is a non-owning iterable range over characters with many of the utilities and properties you'd expect of a string class, except you cannot insert/delete characters (and editing characters is often blocked in some subtypes).
I would advise trying that approach -- write your own (in your own utility namespace). (You should have your own utility namespace for reusable code snippets anyhow).
The core data is a pair of char*
pr std::string::iterator
s (or const
versions). If the user needs a null terminated buffer, a to_string
method allocates one. I would start with non-mutable (const
) character data. Do not forget begin
and end
: that makes your view iterable with for(:)
loops.
This design has the danger that the original std::string
has to persist long enough to outlast all of the views.
If you are willing to give up some performance for safety, have the view own a std::shared_ptr<const std::string>
that it can move a std::string
into, and as a first step move the entire buffer into it, and then start chopping/parsing it down. (child views make a new shared pointer to same data). Then your view class is more like a non-mutable string with shared storage.
The upsides to the shared_ptr<const>
version include safety, longer lifetime of the views (there is no more lifetime dependency), and you can easily forward your const
"substring" type methods to the std::string
so you can write less code.
Downsides include possible incompatibility with incoming standard one1, and lower performance because you are dragging a shared_ptr
around.
I suspect views and ranges are going to be increasingly important in modern C++ with the upcoming and recent improvements to the language.
boost::string_ref
is apparently an implementation of a proposal to the C++1y standard.
1 however, given how simple it is to add capabilities in template metaprogramming, having a "resource owner" template argument to a view type might be a good design decision. Then you can have owning and non-owning string_view
s with otherwise identical semantics...