cuvidk cuvidk - 1 year ago 151
C++ Question

std::string& vs boost::string_ref

Does it matter anymore if I use

? I mean, is it really more efficient to use
over the std version when you are processing strings ? I don't really get the explanation offered here: . What really confuses me is the fact that
is also a handle class that only points to the allocated memory, and since c++11, with move semantics the copy operations noted in the article above are not going to happen. So, which one is more efficient ?

Answer Source

I have never used it be it seems to me that its purpose is to provide an interface similar to std::string but without having to allocate a string for manipulation. Take the example given extract_part(): it is given a hard-coded C array "ABCDEFG", but because the initial function takes a std::string an allocation takes place (std::string will have its own version of "ABCDEFG"). Using string_ref, no allocation occurs, it uses the reference to the initial "ABCDEFG". The constraint is that the string is read-only.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download