Implement N3762 string_view: a non-owning reference to a string.

Jonathan Wakely jwakely.gcc@gmail.com
Sat Nov 16 16:06:00 GMT 2013


On 16 November 2013 15:39, Ed Smith-Rowland wrote:
> Here is a new patch that I believe answers all questions.

Thanks.

> Fixed spelling.

The new doxygen header in the .tcc file still says experimantal :-)

+/** @file experimantal/string_view.tcc

Also both headers and all the tests refer to Clause 21 in the
standard, but of course <string_view> isn't going to be there now:

+// ISO C++ 14882: 21  Strings library

Apart from those minor issues (I hope the tests will be relatively
easy to fix with sed) this is OK to commit.  Some more responses
below, but no more requests for changes ...


> _S_compare I did the same trick as with at and substr to get constexpr.
> It's sloppy because since we don't have C++14 constexpr yet I can't store a
> variable for __diff.  The repeated difference calculation will either be
> done at compile time or hopefully optimized out.

An alternative approach is to dispatch to another constexpr function:

static constexpr const int
_S_compare(size_type __n1, size_type __n2) noexcept
{
  return _S_compare(__n1, __n2, __n2 - __n1);
}

static constexpr const int
_S_compare(size_type __n1, size_type __n2, difference_type __d) noexcept
{
 return __d > std::numeric_limits<int>::max()
     ? std::numeric_limits<int>::max()
     : __d  < std::numeric_limits<int>::min()
     ? std::numeric_limits<int>::min()
     : static_cast<int>(__d);
}

But I agree the repeated calculation should be optimized away so it's
simpler as a single function. It can use a local variable when the
front-end supports it.

> Open issues:
> I added operator""sv on my own initiative.  This has been mentioned as a
> possibility in all drafts that I've seen but it's still an addition.

I think that's OK for an <experimental/xxx> header.

> The papers have =default copy ctor and copy assign.  Why not the move ctor
> and move assign?

Probably because the member variables are simple built-in types, so by
default a move is identical to a copy anyway. You could provide
user-defined move ops which zero out the members of the moved-from
value, but that would make a move slower than a copy!



More information about the Libstdc++ mailing list