In C++11 what is the most performant way to return a reference/pointer to a position in a std::string?
c++, c++11, stdstring
Solution
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...
Problem
I'm building a text parser that uses `std::string` as the core storage for strings. I know this is not optimal and that parsers inside compilers use optimzed approaches for this. In my project I don't mind losing some performance in exchange for more clarity and easier maintenance. At the beginning I read a huge text into memory and then I scan each character to build a ordered set of tokens, its a simple lexer. Currently I'm using `std::string` to represent the text of a token but I would like to improve this a bit by using a reference/pointer into the original text. From what I have read it is a bad practice to return and hold to iterators and it is also a bad practice to refer to the `std::string` internal buffer. Any suggestions on how to accomplish this in a "clean" way?