This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Implement N3762 string_view: a non-owning reference to a string.
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Ed Smith-Rowland <3dw4rd at verizon dot net>
- Cc: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>, Paolo Carlini <pcarlini at gmail dot com>
- Date: Sat, 16 Nov 2013 16:06:02 +0000
- Subject: Re: Implement N3762 string_view: a non-owning reference to a string.
- Authentication-results: sourceware.org; auth=none
- References: <5285A634 dot 7070907 at verizon dot net> <CAH6eHdRFaGKO-6sBhhxip55k65-iS34=_1PWJWCYxBOJ99tB+Q at mail dot gmail dot com> <528791CA dot 1020006 at verizon dot net>
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!